This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Operators – 2”.
1. What will be the output of the following PHP code?
<?php
$i = 0;
while ($i = 10)
{
print "hi";
}
print "hello";
?>
a) hello
b) infinite loop
c) hihello
d) error
View Answer
Explanation: While condition always gives 1.
2. What will be the output of the following PHP code?
<?php
$i = "";
while ($i = 10)
{
print "hi";
}
print "hello";
?>
a) hello
b) infinite loop
c) hihello
d) error
View Answer
Explanation: While condition always gives 1.
3. What will be the output of the following PHP code?
<?php
$i = 5;
while (--$i > 0)
{
$i++;
print $i;
print "hello";
}
?>
a) 4hello4hello4hello4hello4hello…..infinite
b) 5hello5hello5hello5hello5hello…..infinite
c) no output
d) error
View Answer
Explanation: The given PHP code results in an infinite loop, continuously printing “5hello”. This happens because $i is first decremented in the while condition but then immediately incremented inside the loop, bringing it back to 5. Since $i never decreases permanently, the loop never terminates, causing an endless repetition of “5hello”.
4. What will be the output of the following PHP code?
<?php
$i = 5;
while (--$i > 0 && ++$i)
{
print $i;
}
?>
a) 5
b) 555555555…infinitely
c) 54321
d) error
View Answer
Explanation: As it is && operator it is being incremented and decremented continuously.
5. What will be the output of the following PHP code?
<?php
$i = 5;
while (--$i > 0 || ++$i)
{
print $i;
}
?>
a) 54321111111….infinitely
b) 555555555…infinitely
c) 54321
d) 5
View Answer
Explanation: As it is || operator the second expression is not evaluated till i becomes 1 then it goes into a loop.
6. What will be the output of the following PHP code?
<?php
$i = 0;
while(++$i || --$i)
{
print $i;
}
?>
a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) 1
d) 0
View Answer
Explanation: As it is || operator the second expression is not evaluated and i is always incremented, in the first case to 1.
7. What will be the output of the following PHP code?
<?php
$i = 0;
while (++$i && --$i)
{
print $i;
}
?>
a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) no output
d) error
View Answer
Explanation: The first condition itself fails thus the loop exits.
8. What will be the output of the following PHP code?
<?php
$i = 0;
while ((--$i > ++$i) - 1)
{
print $i;
}
?>
a) 00000000000000000000….infinitely
b) -1-1-1-1-1-1-1-1-1-1…infinitely
c) no output
d) error
View Answer
Explanation: (–$i > ++$i) evaluates to 0 but -1 makes it enters the loop and prints i which is 0.
9. What will be the output of the following PHP code?
<?php
$i = 2;
while (++$i)
{
while ($i --> 0)
print $i;
}
?>
a) 210
b) 10
c) no output
d) infinite loop
View Answer
Explanation: The loop ends when i becomes 0.
10. What will be the output of the following PHP code?
<?php
$i = 2;
while (++$i)
{
while (--$i > 0)
print $i;
}
?>
a) 210
b) 10
c) no output
d) infinite loop
View Answer
Explanation: The loop never ends as i is always incremented and then decremented.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Apply for Programming Internship
- Practice Programming MCQs
- Practice MCA MCQs
- Check Information Technology Books
- Check PHP Books