Skip to main content

22. Tricky Questions

What is the difference between == and ===?​

Junior

Answer:

  • ==: Equality (checks value, performs type juggling).
  • ===: Identity (checks both value and type).

What happens when you compare "10" and 10 using ==?​

Junior

Answer: It returns true because of type juggling.


What does the following code output?​

Junior
function foo(&$var) {
$var++;
}
$a = 5;
foo($a);
echo $a;

Answer: 6. The & operator passes the variable by reference, so the modification inside the function affects the original variable.


What is the difference between array_merge() and the + operator for arrays?​

Middle

Answer:

  • array_merge(): For string keys, the latter value overrides the former. For numeric keys, values are appended and re-indexed.
  • +: For string keys, the former value is kept, and the latter is ignored. For numeric keys, it also keeps the former.

How do static properties behave in inherited classes?​

Middle

Answer: Static properties are shared among all classes in an inheritance hierarchy unless they are explicitly overridden in a child class.


Why is it risky to use PHP as a daemon?​

Senior

Answer: PHP was originally designed for short-lived requests. In a long-lived process, memory leaks in your code or libraries will eventually crash the process.