This set of Tricky PHP Interview Questions & Answers focuses on “While Loops – 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: i is decremented in the while loop in the condition check and then incremented back.
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) 4321111111….infinitely
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 exists.
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.
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 tricky interview questions on PHP, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice MCA MCQs
- Practice Programming MCQs
- Check PHP Books
- Check Information Technology Books
- Apply for Programming Internship