PHP Test online
11. Consider the following segment of code:Вариант ответа 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 Comments: 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. |
Comments
| User name | Comment |
|---|
Русский