PHP Multiple Choice Questions – While Loops

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “While Loops”.

1. What will be the output of the following PHP code?

  1. <?php
  2. while()
  3. {
  4.     print "hi";
  5. }
  6. ?>

a) infinite loop
b) hi
c) no output
d) error
View Answer

Answer: d
Explanation: The while loop cannot be defined without a condition.
advertisement
advertisement

2. What will be the output of the following PHP code?

  1. <?php
  2. do
  3. {
  4.     print "hi";
  5. }
  6. while(0);
  7. print "hello";
  8. ?>

a) infinite loop
b) hihello
c) hello
d) error
View Answer

Answer: b
Explanation: The do while loop executes at least once as the condition is in the while loop.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What will be the output of the following PHP code?

advertisement
  1. <?php
  2. $i = 0
  3. do
  4. {
  5.     print "hi";
  6.     $i++;
  7. }
  8. while ($i != 3);
  9. ?>

a)

   hi
   hi
advertisement

b) hi
c)

   hi
   hi
   hi
   hi

d) no output
View Answer

Answer: c
Explanation: The check happens after the increment,thus it prints until i = 4.

4. What will be the output of the following PHP code?

  1. <?php
  2. $i = 0
  3. while ($i != 3)
  4. {
  5.     print "hi";
  6. 	$i++;
  7. }
  8. ?>

a)

   hi
   hi

b)

   hi
   hi
   hi

c)

   hi
   hi
   hi
   hi

d) no output
View Answer

Answer: b
Explanation: The check happens before the increment, thus it prints until i = 3.

5. What will be the output of the following PHP code?

  1. <?php
  2. $i = 0
  3. while ($i < 3)
  4. {
  5.     print "hi";
  6.     $i--;
  7. }
  8. print "hello"
  9. ?>

a)

   hi
   hi
   hello

b)

   hi
   hi
   hi
   hello

c)

   hi
   hi
   hi
   hi
   hello 

d) infinite loop
View Answer

Answer: d
Explanation: There is no increment of i making it infinite.

6. What will be the output of the following PHP code?

  1. <?php
  2. $i = 0
  3. while ($i < 3)
  4. {
  5.     $i++;
  6. }
  7. print $i;
  8. ?>

a) 2
b) 3
c) 0
d) 1
View Answer

Answer: b
Explanation: The increment happens and then the check happens.

7. What will be the output of the following PHP code?

  1. <?php
  2. $i = 0
  3. do
  4. {
  5.     $i++;
  6. }
  7. while ($i < 3);
  8. print $i;
  9. ?>

a) 2
b) 3
c) 0
d) 1
View Answer

Answer: b
Explanation: The increment happens and then the check happens.

8. What will be the output of the following PHP code?

  1. <?php
  2. $i = 0
  3. while ($i++)
  4. {
  5.     print $i;
  6. }
  7. print $i;
  8. ?>

a) 0
b) infinite loop
c) 01
d) 1
View Answer

Answer: d
Explanation: As it is a post increment, it checks and then does not enter the loop, thus prints only 1.

9. What will be the output of the following PHP code?

  1. <?php
  2. $i = "";
  3. while($i)
  4. {   
  5.     print "hi";
  6. }
  7. print "hello";
  8. ?>

a) hello
b) infinite loop
c) hihello
d) error
View Answer

Answer: a
Explanation: $i is null string which evaluates to false in the while loop and hence the statements inside the while loop won’t be executed.

10. What will be the output of the following PHP code?

  1. <?php
  2. $i = "";
  3. while ($i)
  4. {   
  5.     print "hi";
  6. } 
  7. while($i < 8)
  8.     $i++;
  9. print "hello";
  10. ?>

a) hi is printed 8 times, hello 7 times and then hi 2 times
b) hi is printed 10 times, hello 7 times
c) hi is printed once, hello 7 times
d) hello
View Answer

Answer: d
Explanation: $i is null string which evaluates to false in the 1st while loop and hence the statements inside the 1st while loop won’t be executed. In the 2nd while loop, it increments 8 times, and then it prints “hello” once.

More MCQs on PHP While Loops:

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.