7. Which values should be assigned to the variables $a, $b and $c in order for the following scrīpt to display the string Hello, World!?
<?php
$string = "Hello, World!";
$a = ?;
$b = ?;
$c = ?;
if($a) {
if($b && !$c) {
echo "Goodbye Cruel World!";
} else if(!$b && !$c) {
echo "Nothing here";
}
} else {
if(!$b) {
if(!$a && (!$b && $c)) {
echo "Hello, World!";
} else {
echo "Goodbye World!";
}
} else {
echo "Not quite.";
}
}
?>
A. False, True, False
B. True, True, False
C. False, True, True
D. False, False, True
E. True, True, True
Answer: Following the logic of the conditions, the only way to get to the Hello, World! string is in the else condition of the first if statement. Thus, $a must be False. Likewise, $b must be False. The final conditional relies on both previous conditions ($a and $b) being False, but insists that $c be True (Answer D).
8. What will the following scrīpt output?
<?php
$array = '0123456789ABCDEFG';
$s = '';
for ($i = 1; $i < 50; $i++) {
$s .= $array[rand(0,strlen ($array) - 1)];
}
echo $s;
?>
A. A string of 50 random characters
B. A string of 49 copies of the same character, because the random number generator has not been initialized
C. A string of 49 random characters
D. Nothing, because $array is not an array
E. A string of 49 ‘G’ characters
Answer: The correct answer is C. As of PHP 4.2.0, there is no need to initialize the random number generator using srand() unless a specific sequence of pseudorandom numbers is sought. Besides, even if the random number generator had not been seeded, the scrīpt would have still outputted 49 pseudo-random characters—the same ones every time. The $array variable,
though a string, can be accessed as an array, in which case the individual characters corresponding to the numeric index used will be returned. Finally, the for loop starts from 1 and continues until $i is less than 50—for a total of 49 times.