5 PHP Interview Questions Series

Question 1: What will be the output of the code below?

The output is:  bangladesh does not contain bangla

 

So what is the explanation?

The problem here is that strpos()  returns the starting position index of $str1 in $str2 (if found), otherwise it returns false. So in this example, strpos()  returns 0 (which is then coerced to false when referenced in the if statement).

 

Question 2: What will be the output of the code below and why?

The output is:
In the snippet of code there are two key facts that we need to keep in mind:

  • The term $x++  says to use the current value of $x  and then increment it. Similarly, the term $x--  says to use the current value of $x  and then decrement it.
  • The increment operator (++)  has higher precedence then the sum operator (+)  in order of operations.

 

Question 3: Explain the role of $a and $b in the snippet of code:

Both $a  and $b  will be equal to the string “21” after the above code is executed. Because the statement $b = &$a;  sets $b  equal to a reference to $a  (as opposed to setting $b  to the then-current value of $a ). Thereafter, as long as $b  remains a reference to $a , anything done to will affect $b  and vice versa.

So when we subsequently execute the statement $b = "2$b" , $b  is set equal to the string “2” followed by the then-current value of $b  (which is the same as $a ) which is 1, so this results in $b  being set equal to the string “21” (i.e., the concatenation of “2” and “1”). And, since $b is a reference to $a , this has the same affect on the value of $a , so both end up equal to “21”.

 

Question 4: What will be the output of each of the statements below and why?

var_dump(0123 == 123)  will output bool(false) because the leading 0 in 0123 tells the PHP interpreter to treat the value as octal (rather than decimal) value, and 123 octal is equal to 83 decimal, so the values are not equal.

var_dump('0123' == 123)  will output bool(true) since the string 0123 will automatically be coerced to an integer when being compared with an integer value. Interestingly, when this conversion is performed, the leading 0 is ignored and the value is treated as a decimal (rather than octal) value, so the values are bother 123 (decimal) and are therefore equal.

var_dump('0123' === 123)  outputs bool(false) since it performs a more strict comparison and does not do the automatic type coercion of the string to an integer.

 

Question 5: What is the problem with the code below? What will it output? How can it be fixed?

The output will be as follows:
You may also see two warnings generated, similar to the following:
The issue here is that, if either the first or second argument to array_merge()  is not an array, the return value will be NULL. For example, although one might reasonably expect that a call such as array_merge($someValidArray, NULL)  would simply return $someValidArray , it instead returns NULL!

As a result, the call to $testArray = array_merge($testArray, $referenceTable[‘val2’]) evaluates to $testArray = array_merge($testArray, 3) and, since 3 is not of type array, this call to array_merge() returns NULL, which in turn ends up setting $testArray equal to NULL. Then, when we get to the next call to array_merge(), $testArray is now NULL so array_merge() again returns NULL. (This also explains why the first warning complains about argument #2 and the second warning complains about argument #1.)

The fix for this is straightforward. If we simply typecast the second argument to an array, we will get the desired results. The corrected array_merge() calls would therefore be as follows:

which will yield the following output (and no warnings):

 

Share with:

Categories: PHP