December Code

PHP for placements: interview questions by topic

PHP still runs a large share of the web, from WordPress sites to Laravel and Symfony applications, and service companies and web agencies hire for it in every placement season. PHP interviews have a reputation for trick questions, and there is a reason: the language converts types freely, its arrays do the work of several data structures, and some of its oldest behaviour changed in PHP 8. A candidate who learned from old tutorials can give answers that were right in 2015 and are wrong today.

The questions therefore test the rules under the syntax: when == converts and when === does not, how arrays are copied and merged, how closures capture variables, what self, static and parent refer to, how traits and interfaces combine, and which failures are Exceptions and which are Errors. Modern features such as match, named arguments and constructor promotion come up too, because they show whether someone writes current PHP.

The pages below take one topic each, with answers whose code was run on PHP 8.5, and one question exactly as the diagnostic asks it. The diagnostic draws from every PHP topic in the bank and names the ones to read first.

New to this? How to prepare for placements — the method — and what the readiness test measures.

PHP topics, one page each

8 of the 8 PHP topics in the bank have a page so far; the diagnostic draws from all 8.

  1. PHP data types interview questions

    PHP types and operators for interviews: the type list, == versus ===, PHP 8 comparisons, isset and empty, ??, the spaceship and floats.

  2. PHP string interview questions

    PHP strings for interviews: single and double quotes, heredoc and nowdoc, searching, comparing, splitting, UTF-8 lengths and sprintf formats.

  3. PHP array interview questions

    PHP arrays for interviews: copy-on-write, array_merge versus +, map, filter and reduce, sorting, key checks, destructuring and spreading.

  4. PHP functions and closures interview questions

    PHP functions for interviews: closures with use, arrow functions, default, named and variadic parameters, references, types and static variables.

  5. PHP OOP interview questions

    PHP object-oriented programming for interviews: constructors and promotion, visibility, self, static and parent, statics, comparison, DI.

  6. PHP traits and interfaces interview questions

    PHP interfaces and traits for interviews: interfaces, abstract classes, traits and their rules, several interfaces, Countable and introspection.

  7. PHP exception handling interview questions

    PHP error handling for interviews: try, catch and finally, Exception versus Error, custom exceptions, multi-catch, chaining and the @ operator.

  8. PHP 8 features interview questions

    PHP 8 features for interviews: match, named arguments, constructor promotion, the nullsafe operator, union types, attributes and the JIT.

One question, exactly as the diagnostic asks it

From the PHP bank — 30 questions across 8 topics. The answer is marked because this one is public; in a sitting you choose first, then see why each option is right or wrong.

Classes & Objects · mediumPHP-016

What does this PHP code print?

class A {
    public static function make() {
        return new static();
    }
    public static function makeSelf() {
        return new self();
    }
}
class B extends A {}
echo get_class(B::make()), " ", get_class(B::makeSelf());
  1. 1A A
  2. 2B B
  3. 3B Acorrect
  4. 4A B

self always refers to the class in which the method is written, A, so new self() creates an A even when called as B::makeSelf(). static is resolved at run time to the class the method was called on, which is late static binding: B::make() runs A's make with static meaning B, so it creates a B. The output is B A. A A treats static like self. B B treats self like static. A B has them the wrong way round. new static is how factory methods in a base class build the right subclass.

More on this topic: PHP OOP interview questions

Measure it

Reading the answers tells you what’s true. A diagnostic tells you what you get wrong.

10 PHP questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.

By Harshit · updated