Question:Consider the following segment of code:<?php
define("STOP_AT", 1024);
$result= array();
/* Missing code */
{
$result[] = $idx;
}
print_r($result);
?>
What should go in the marked segment to produce the following array output?
Array
{
[0] => 1
[1] => 2
[2] => 4
[3] => 8
[4] => 16
[5] => 32
[6] => 64
[7] => 128
[8] => 256
[9] => 512
}
+ ExplanationAs it is only possible to add a single line of code to the segment provided, the only statement that makes sense is a for loop, making the choice either C or D. In order to select the for loop that actually produces the correct result, we must first of all revisit its structural elements. In PHP, for loops are declared as follows:
for (; ;)
where the is executed prior to entering the loop. The for loop then begins executing the code within its code block until the statement evaluates to False. Every time an iteration of the loop is completed, the is executed.
Applying this to our code segment, the correct for statement is:
for ($idx = 1; $idx < STOP_AT; $idx *= 2)
or answer C.