PHP Coding Questions and Answers – For Loops – 3

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

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

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

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

Answer: c
Explanation: The condition of the loop is always false 0.
advertisement
advertisement

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

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

a) infinite loop
b) 0 1 2 3 4 5
c) 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5 0 1 2 3 4 5
d) no output
View Answer

Answer: a
Explanation: In the second loop j value is not being changed.
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. for ($i = 0; $i < 5; $i++)  
  3. {
  4.     for ($j = $i;$j > $i; $i--)
  5.         print $i;
  6. }
  7. ?>

a) infinite loop
b) 0 1 2 3 4 5
c) 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5 0 1 2 3 4 5
d) no output
View Answer

Answer: d
Explanation: The second loop does not execute as the check condition is always false.
advertisement

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

  1. <?php
  2. $user = array("Ashley", "Bale", "Shrek", "Blank");
  3. for ($x = 0; $x < count($user); $x++) 
  4. {
  5.     if ($user[$x] == "Shrek") 
  6. 	    continue;
  7.     printf ($user[$x]); 
  8. }
  9. ?>

a) AshleyBaleBlank
b) AshleyBale
c) AshleyBaleShrek
d) No output
View Answer

Answer: a
Explanation: Only the Shrek is skipped due to the continue statement.

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

  1. <?php
  2. $user = array("Ashley", "Bale", "Shrek", "Blank");
  3.     for ($x=0; $x < count($user) - 1; $x++)	
  4.     {
  5.         if ($user[$x++] == "Shrek") 
  6. 		    continue;
  7.         printf ($user[$x]); 
  8.     }
  9. ?>

a) AshleyBaleBlank
b) Bale
c) AshleyShrek
d) BaleBlank
View Answer

Answer: a
Explanation: Only Bale is printed as $x++ is done before printing and then checked.

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

  1. <?php
  2. $user = array("Ashley", "Bale", "Shrek", "Blank");
  3. for ($x = 0; $x < count($user); $x) 
  4. {
  5.     if ($user[$x++] == "Shrek") 
  6. 	    continue;
  7.     printf ($user[$x]); 
  8. }
  9. ?>

a) AshleyBaleBlank
b) BaleShrek
c) AshleyBlank
d) Bale
View Answer

Answer: b
Explanation: x is incremented only inside loop i the if condition.

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

  1. <?php
  2. for ($i = 0; $i % ++$i; $i++) 
  3. {
  4.     print"i";
  5. }
  6. ?>

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

Answer: b
Explanation: Loop condition is true as i%(i+1) is a float non zero value in php.

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

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

a) iiiii
b) infinite loop
c) iiiiiiiiiiiiiiiiiiiiiiiii
d) no output
View Answer

Answer: a
Explanation: The i value is changed in the inner loop and reaches five, thus does not execute the second outer loop.

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

  1. <?php
  2. $a = array("hi", "hello", "bye");
  3. foreach ($a as $value) 
  4. {
  5.     if (count($a) == 2)
  6. 	    print $value;         
  7. }
  8. ?>

a) hihellobye
b) infinite loop
c) hihello
d) no output
View Answer

Answer: d
Explanation: As count($a) returns 3 the condition is always false.

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

  1. <?php
  2. $a = array("hi", "hello", "bye");
  3. for (;count($a) < 5;) 
  4. {
  5.     if (count($a) == 3)
  6. 	    print $a;         
  7. }
  8. ?>

a) ArrayArrayArrayArrayArrayArray….infinitely
b) (“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)…infinitely
c) hihellobyehihellobyehihellobyehihellobyehihellobyehihellobye…..infinitely
d) no output
View Answer

Answer: a
Explanation: As count($a) returns 3 the condition is always true, thus it prints $a, which returns its data type.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of 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]

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.