PHP While Loops
- What will be the output of the following PHP code ?
<?php
$m = 8;
while (--$m > 0 || ++$m)
{
print $m;
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
As it is || operator the second expression is not evaluated till m becomes 1 then it goes into a loop.
- What will be the output of the following PHP code ?
<?php
$n = 1;
while(++$n || --$n)
{
print $n;
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
As it is || operator the second expression is not evaluated and n is always incremented, in the first case to 1.
- What will be the output of the following PHP code ?
<?php
$num = 0;
while (++$num && --$num)
{
print $num;
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
The first condition itself fails thus the loop exists.
- What will be the output of the following PHP code ?
<?php
$t = 0;
while ((--$t > ++$t) - 1)
{
print $t;
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
(–$t > ++$t) evaluates to 0 but -1 makes it enters the loop and prints t.
- What will be the output of the following PHP code ?
<?php
$number = 5;
while (++$number)
{
while ($number --> 0)
print $number;
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
The loop ends when number becomes 0.