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