250+ TOP PHP Coding MCQs on For Loops and Answers

PHP Multiple Choice Questions on “For Loops – 1”.

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

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

a) 0
b) infinite loop
c) no output
d) error
Answer: d
Clarification: Wrong syntax for for loop.

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

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

a)

red 
  green 
  blue 
  yellow

b) red
c) no output
d) error
Answer: a
Clarification: This runs a for loop for that array.

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

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

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

b) The number is: 0
c) no output
d) error
Answer: a
Clarification: This runs a for loop from 0 to 10.

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

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

a) 123456789101112
b) 12345678910
c) 1234567891011
d) infinite loop
Answer: a
Clarification: 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. for ($x = 1; $x < 10;++$x)
  2. {
  3.     print "*t";
  4. }
  5. ?>

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

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

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

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

a) 123456789101112
b) 12345678910
c) 1234567891011
d) infinite loop
Answer: d
Clarification: The value of x is decremented thus making it an infinite loop.

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

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

a) -3-4-5
b) -3-4
c) infinite loop
d) no output
Answer: d
Clarification: The loop is not even entered as x is initially 0.

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

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

a) In for loop
b) After for loop
c) In for loopAfter for loop
d) Infinite loop
Answer: c
Clarification: The loop runs only once as value of x is incremented.

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

  1. for (1; $i == 1; $i = 2)
  2.     print "In for loop ";
  3. print "After loopn";
  4. ?>

a) In for loop
b) After for loop
c) In for loopAfter for loop
d) Infinite loop
Answer: b
Clarification: 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. for ($i == 2; ++$i == $i; ++$i)
  2.     print "In for loop ";
  3. print "After loopn";
  4. ?>

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

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top