11. 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
}
Вариант ответа 1 - A) foreach($result as $key => $val)
Вариант ответа 2 - B) while($idx *= 2)
Вариант ответа 3 - C) for($idx = 1; $idx < STOP_AT; $idx *= 2)
Вариант ответа 4 - D) for($idx *= 2; STOP_AT >= $idx; $idx = 0)
Вариант ответа 5 - E) while($idx < STOP_AT) do $idx *= 2
Комментарий: As 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 (<init statement>; <continue until statement>; <iteration statement>) where the <init statement> is executed prior to entering the loop. The for loop then begins executing the code within its code block until the <continue until> statement evaluates to False. Every time an iteration of the loop is completed, the <iteration statement> is executed. Applying this to our code segment, the correct for statement is: for ($idx = 1; $idx < STOP_AT; $idx *= 2) or answer C.