Thiemos Archiv

There are many lists of »interview questions for web developers«. They are popular not only to literally prepare for a job interview. It can be a fun challenge just to see how good you perform. This got me hooked when a post titled »PHP interview questions and answers for web developers« popped up in my Twitter timeline. It stood out for reasons you will understand shortly.

The post is written by a user with the suspicious name »Devinterview-io«. And indeed, the post is effectively a teaser for a paid service. We get 15 questions and answers for free, and can find some more on the website. The majority of the 82 answers is locked. But this should not stop us.

Note the following is written from my perspective as a MediaWiki developer with the MediaWiki coding conventions in mind. Even if PHP is old – and you will still find a lot of old code in the MediaWiki code base – the language evolves and started to catch up again. I try hard to make all code I touch as modern as possible. Which mostly means isolation, isolation, isolation.

Ok, let's have a look at what I think are the most controversial interview questions for PHP developers.

2. What is the difference between == and ===?
One is strict, the other is not. The best advice I can give is to always use ===, except you know you have to use ==, e.g. when you want to check if two objects contain the same values.
3. What is the return type of a function that doesn't return anything?
We call the type void. But what the function actually returns is null, just as if it ended in return null. There is no way to distinguish these from the outside, as far as I know.
4. What does $GLOBALS mean?
It means you are looking at bad code – technical debt you need to track and eventually pay for. Please make sure at least 99% of your code doesn't contain $GLOBALS or the global keyword.
9. What is stdClass in PHP?
It's a generic value object with no methods. It's what functions like mysqli_fetch_object() return. It's what you get when casting an array to an object. But it's not the common base class for all objects in PHP. This subtle difference is a nice to know detail when you start type-hinting your code with object – which you can do since PHP 7.2. It means stdClass is more narrow – more specific – and you should prefer it as a type hint when it makes sense.
10. In PHP, objects are they passed by value or by reference?
By reference, obviously. This was different in PHP 3 and is the reason why there are still many function headers with &$object parameters in MediaWiki code. The additional & forced PHP 3 to pass an object reference instead of making a crazy expensive copy of the object. Almost all of these & don't make sense any more, but can't easily be removed for compatibility reasons.

But wait. states »In PHP, objects passed by value« and cites as a source, which doesn't explain anything. Oh no, not again. While it's possible to say that »object references are passed by value«, this is pedantic bullshit. Seriously, don't be this guy. Here is a long StackOverflow post going into a lot more detail.

Question 10, and the site already lost all credibility. *sight* Let's move on.

12. Is there a difference between isset and !empty?
Yes. Quite a big one actually. isset() is only false if a variable or array element doesn't exist or is null. empty() on the other hand considers all »falsy« values empty, including empty arrays and even the string "0".

You might think this doesn't make a difference in all cases you can think of, and you might be right. The thing is: this will hit you when you don't expect it. MediaWiki for example is notorious for not properly supporting the page title or user name »0«. For this reason we avoid empty() entirely, and prefer strict === over relaxed if ( $var == null ) or if ( $var ) whenever we can.

13. Differentiate between echo and print()
No. Sorry. What kind of PHP does your company maintain? My code doesn't contain either. Except it's a command line tool (CLI). And even then I don't care.
15. What does the 'var' keyword mean in PHP?
It means your code is outdated. You shouldn't use var any more, but mark class properties with private, public, and sparingly protected. In MediaWiki we actively disallow var for a long, log time now.
17. What are the differences between die() and exit() functions in PHP?
This is a trick question. There is none. Here is a really good StackOverflow post going into some details.
19. Explain usage of enumerations in PHP
There are none, but will be in PHP 8.1. The relevant RFC was accepted just a few days ago.

Until then we can either have a bunch of constants in the main namespace that share a common prefix (MediaWiki's NS_… constants are an example), or wrap a set of public constants in an interface (here is an example from MediaWiki).

28. What is the difference between var_dump() and print_r()?
This is not an interview question. Both dump something in some format. Just another one was discussed for PHP 8.1, but declined.
29. What is the difference between single-quoted and double-quoted strings in PHP?
Not performance.
31. Give me some real life examples when you had to use __destruct in your classes
34. Is there a function to make a copy of a PHP array to another?
Haha, yes: $copy = $original. Seriously. PHP does »copy-on-write«, which means it only makes an actual copy of the array the moment your code tries to change one of the two.

I fear this is meant as a trick question, and they want you to do some weird stuff with array_slice(). No, thanks.

37. Maximum how many arguments are allowed in a function in PHP?
One. You should have a good reason to have more arguments in a function. When you have more than 3, you are doing something wrong and should reconsider your code architecture.

But to actually answer the question: no, there is none. And even if, who cares? No code in the world should have so many arguments that you are worried about a maximum.

41. Differentiate between exception and error
These are two entirely different things in PHP. They are currently trying to clean this mess up and turn as many errors as possible into exceptions. This takes time because people need to adapt.
43. What are the exception class functions?
The what?
46. Why do we use extract()?
Excuse me? We don't. We even actively block it.
54. Is there any reason to use strcmp() for strings comparison?
Sure. In a sort function.
61. Check if PHP array is associative
They all are. What you really meant is: Check if a PHP array is a straight list with nothing but sequential integer keys, and no gaps (i.e. not sparse). There are clever ways to do checks like this, but they are expensive and barely ever needed. Prefer foreach () over for () loops whenever you can, and you typically don't need to care. If you do, have a native $array = array_values( $array ) in front of the critical code path.

PHP 8.1 will even add a native is_list() function that replaces clever but expensive workarounds like $array === array_values( $array ).

63. Store an array as JSON or as a PHP serialized array?
Please, never persist PHP serializations. Not even temporary. We did this in the Wikibase code base. It was painful. Why? PHP's serialization format contains full qualified class names. Which means you can never change this part of your codebase any more.

Always use JSON.

69. What is the best method to merge two PHP objects?
To do what? You don't merge objects. Maybe arrays, but not objects.
72. What does $$ mean?
It means your code is badly readable and you should rewrite it with less clever trickery.
73. What is the crucial difference between using traits versus interfaces?
Erm, … seriously? These are two entirely different things that barely have anything in common, other than being part of OOP.
75. What's better at freeing memory with PHP: unset() or $var = null?
There is not really one, at least none that matters. What matters is that both release whatever was in the variable before, to be picked up by the garbadge collection. Setting a variable to null keeps the variable in the current scope, just empty. You can continue to use it in e.g. if ( $var !== null ). When you try to do the same after unset() you run into an error, because the variable is not known any more. Sure, this might release a few extra bytes of memory a bit earlier. Do you care?
79. What does a $$$ mean in PHP?
Holy crap! Yea, this is valid PHP syntax:
$a = 'result';
$b = 'a';
$c = 'b';
echo $$$c; // prints "result"

But seriously: leave the room if this is an interview question.

81. How could we implement method overloading in PHP?
That's a good one. Doing it is not that hard. The most common and typically accepted way is to have optional parameters you can omit, starting from the end. You can also specify the method with no parameters and then process the array you get from func_get_args() to distinguish between the different ways your method can be called. More recent PHP should use the function ( ...$arguments ) syntax, called »variable-length argument lists«.

What makes this hard is the question how to document such a method? Because the logic to distinguish the different calls is hidden within the method and not easily visible in the method's signature. You need to document it. But there is no good, universally understood way to do this. Better use multiple methods with different names.

PHP 8 significantly improves this situation with named arguments.

Kommentare zu diesem Beitrag können per E-Mail an den Autor gesandt werden.

[ ← Zurück zur Übersicht ]

Impressum & Datenschutz