Question 1: What will be the output of the code below?
<?php $str1 = 'bangladesh'; $str2 = 'bangla'; if (strpos($str1,$str2)) { echo "\"" . $str1 . "\" contains \"" . $str2 . "\""; } else { echo "\"" . $str1 . "\" does not contain \"" . $str2 . "\""; } ?>
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?
<?php $x = 5; echo $x; echo "<br />"; echo $x+++$x++; echo "<br />"; echo $x; echo "<br />"; echo $x---$x--; echo "<br />"; echo $x; ?>
The output is:
5 11 7 1 5
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:
<?php $a = '1'; $b = &$a; $b = "2$b"; ?>
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?
<?php var_dump(0123 == 123); var_dump('0123' == 123); var_dump('0123' === 123); ?>
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?
<?php $referenceTable = array(); $referenceTable['val1'] = array(1, 2); $referenceTable['val2'] = 3; $referenceTable['val3'] = array(4, 5); $testArray = array(); $testArray = array_merge($testArray, $referenceTable['val1']); var_dump($testArray); $testArray = array_merge($testArray, $referenceTable['val2']); var_dump($testArray); $testArray = array_merge($testArray, $referenceTable['val3']); var_dump($testArray); ?>
The output will be as follows:
array(2) { [0]=> int(1) [1]=> int(2) } NULL NULL
You may also see two warnings generated, similar to the following:
Warning: array_merge(): Argument #2 is not an array Warning: array_merge(): Argument #1 is not an array
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:
<?php $testArray = array_merge($testArray, (array)$referenceTable['val1']); var_dump($testArray); $testArray = array_merge($testArray, (array)$referenceTable['val2']); var_dump($testArray); $testArray = array_merge($testArray, (array)$referenceTable['val3']); var_dump($testArray); ?>
which will yield the following output (and no warnings):
array(2) { [0]=> int(1) [1]=> int(2) } array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }