Control Structures(loop In Php)(Php ) Questions and Answers

Question 1. What will be the output of the following PHP code?
  1.    how are u
  2.    hi
  3.    error
  4.    no output
Explanation:-
Answer: Option A. -> how are u
Uninitialized x is set to 0, thus if condition fails.

Question 2. What will be the output of the following PHP code ?
  1.    hi
  2.    no output
  3.    error
  4.    how are u
Explanation:-
Answer: Option D. -> how are u
x is incremented after if which evaluates to false.

Question 3. What will be the output of the following PHP code ?
  1.    how are uhello
  2.    hihello
  3.    hi
  4.    no output
Explanation:-
Answer: Option B. -> hihello
else condition without brackets performs the following statements only.

Question 4. What will be the output of the following PHP code ?
  1.    true
  2.    false
  3.    error
  4.    no output
Explanation:-
Answer: Option D. -> no output
The nested for loop is not entered if outer condition is false.

Question 5. What will be the output of the following PHP code ?
  1.    true
  2.    false
  3.    error
  4.    no output
Explanation:-
Answer: Option A. -> true
Due to post increment and post decrement only the first condition is satisfied.

Question 6. What will be the output of the following PHP code ?
  1.    hihihi
  2.    hihellohihellohihello
  3.    hellohellohello
  4.    hi
Explanation:-
Answer: Option D. -> hi
When break is encountered it leaves the loop.

Question 7. What will be the output of the following PHP code ?
  1.    hihihi
  2.    hihellohihellohihello
  3.    hellohellohello
  4.    hi
Explanation:-
Answer: Option A. -> hihihi
When continue is encountered it skips to the next iteration

Question 8. What will be the output of the following PHP code ?
  1.    0
  2.    infinite loop
  3.    -1
  4.    1
Explanation:-
Answer: Option C. -> -1
The order of execution is initialization, check, increment/decrement, check, increment/decrement, check, increment/decrement….so on .

Question 9. What will be the output of the following PHP code ?
  1.    24
  2.    134
  3.    1234
  4.    1
Explanation:-
Answer: Option A. -> 24
The order of execution is initialization, check, increment/decrement, check, increment/decrement, check, increment/decrement….so on.

Question 10. What will be the output of the following PHP code ?
  1.    10
  2.    infinite loop
  3.    no output
  4.    error
Explanation:-
Answer: Option B. -> infinite loop
There is no check condition to stop the execution of the loop.