PHP Coding Questions and Answers – For Loops – 1

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

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

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

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

Answer: d
Explanation: Wrong syntax for for loop.
advertisement
advertisement

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

  1. <?php
  2. $colors = array("red","green","blue","yellow"); 
  3. foreach ($colors as $value)
  4. {
  5.     echo "$value <br>";
  6. }
  7. ?>

a)

red 
  green 
  blue 
  yellow
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

b) red
c) no output
d) error
View Answer

Answer: a
Explanation: This runs a for loop for that array.
advertisement

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

  1. <?php
  2. for ($x = 0; $x <= 10; $x++)
  3. {
  4.     echo "The number is: $x <br>";
  5. }
  6. ?>

a)

The number is: 0 
The number is: 1 
The number is: 2 
The number is: 3 
The number is: 4 
The number is: 5 
The number is: 6 
The number is: 7 
The number is: 8 
The number is: 9 
The number is: 10
advertisement

b) The number is: 0
c) no output
d) error
View Answer

Answer: a
Explanation: This runs a for loop from 0 to 10.

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

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

a) 123456789101112
b) 12345678910
c) 1234567891011
d) infinite loop
View Answer

Answer: a
Explanation: The value of x is incremented and printed twice before checking,this last loop it prints 11 and 12.

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

  1. <?php
  2. for ($x = 1; $x < 10;++$x)
  3. {
  4.     print "*\t";
  5. }
  6. ?>

a) **********
b) *********
c) ***********
d) infinite loop
View Answer

Answer: b
Explanation: Loop runs from 1 to 9 i.e 9 times.

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

  1. <?php
  2. for ($x = -1; $x < 10;--$x)
  3. {
  4.     print $x;
  5. }
  6. ?>

a) 123456789101112
b) 12345678910
c) 1234567891011
d) infinite loop
View Answer

Answer: d
Explanation: The value of x is decremented thus making it an infinite loop.

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

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

a) -3-4-5
b) -3-4
c) infinite loop
d) no output
View Answer

Answer: d
Explanation: The loop is not even entered as x is initially 0.

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

  1. <?php
  2. for ($i++; $i == 1; $i = 2)
  3.     print "In for loop ";
  4. print "After loop\n";
  5.  
  6. ?>

a) In for loop
b) After for loop
c) In for loopAfter for loop
d) Infinite loop
View Answer

Answer: c
Explanation: The loop runs only once as value of x is incremented.

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

  1. <?php
  2. for (1; $i == 1; $i = 2)
  3.     print "In for loop ";
  4. print "After loop\n";
  5. ?>

a) In for loop
b) After for loop
c) In for loopAfter for loop
d) Infinite loop
View Answer

Answer: b
Explanation: The loop does not run as i initialized in check statement will be zero.

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

  1. <?php
  2. for ($i == 2; ++$i == $i; ++$i)
  3.     print "In for loop ";
  4. print "After loop\n";
  5. ?>

a) In for loopIn for loopIn for loopIn for loop……infinitely
b) After for loopAfter for loopAfter for loop……..infinitely
c) In for loopAfter for loopIn for loopAfter for loopIn for loopAfter for loop…..infinitely
d) After for loop
View Answer

Answer: a
Explanation: The loop never exits as the condition ++X == X is always satisfied,evaluated from right to left.

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.