The code is as follow-
<?php
$i = 5;
while (--$i > 0 || ++$i)
{
print $i;
}
?>
The correct answer is 4321111.......
. But how could it start from 4, it should just strat from 5. In my opinion, the answer should be 55555555......
.
The code is as follow-
<?php
$i = 5;
while (--$i > 0 || ++$i)
{
print $i;
}
?>
The correct answer is 4321111.......
. But how could it start from 4, it should just strat from 5. In my opinion, the answer should be 55555555......
.
it starts with 4 because this statement is evaluated first (--$i > 0 )
which is pre decrement
, that means it decrements first then evaluates the while loop
so here it is checking whether i is greater than 0 or else increment i by 1
so it prints 4321
, when i reaches 0 the --$i > 0
statement is not evaluated as i becomes 0 so the or part is evaluated which is +1
so the result becomes 4321111111.................