PHP Multiple Choice Questions – If – Else – If

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “If – Else – If”.

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

  1. <?php
  2. $x;
  3. if ($x)
  4.     print "hi" ;
  5. else
  6.     print "how are u";
  7. ?>

a) how are u
b) hi
c) error
d) no output
View Answer

Answer: a
Explanation: Uninitialized x is set to 0, thus if condition fails.
advertisement
advertisement

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

  1. <?php
  2. $x = 0;
  3. if ($x++)
  4.     print "hi";
  5. else
  6.     print "how are u";
  7. ?>

a) hi
b) no output
c) error
d) how are u
View Answer

Answer: d
Explanation: x is incremented after if which evaluates to false.
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. $x;
  3. if ($x == 0)
  4.     print "hi" ;
  5. else
  6.     print "how are u";
  7.     print "hello"
  8. ?>

a) how are uhello
b) hihello
c) hi
d) no output
View Answer

Answer: b
Explanation: else condition without brackets performs the following statements only.
advertisement

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

  1. <?php
  2. $x = 0;
  3. if ($x == 1)
  4.     if ($x >= 0)
  5.         print "true";
  6.     else
  7.         print "false"; 
  8. ?>

a) true
b) false
c) error
d) no output
View Answer

Answer: d
Explanation: The nested for loop is not entered if outer condition is false.

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

  1. <?php
  2. $a = 1;
  3. if ($a--)
  4.     print "True";
  5. if ($a++)
  6.     print "False"; 
  7. ?>

a) true
b) false
c) error
d) no output
View Answer

Answer: a
Explanation: Due to post increment and post decrement only the first condition is satisfied.

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

  1. <?php
  2. $a = 1;
  3. if (echo $a)
  4.     print "True";
  5. else
  6.     print "False"; 
  7. ?>

a) true
b) false
c) error
d) no output
View Answer

Answer: c
Explanation: echo does not return anything so if condition is empty.

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

  1. <?php
  2. $a = 1;
  3. if (print $a)
  4.     print "True";
  5. else
  6.     print "False"; 
  7. ?>

a) 1True
b) True
c) error
d) False
View Answer

Answer: a
Explanation: print returns 1 if it prints anything.

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

  1. <?php
  2. $a = 10;
  3. if (1) 
  4.     print "all";
  5. else 
  6.     print "some"
  7. else 
  8.     print "none";
  9. ?>

a) all
b) some
c) error
d) none
View Answer

Answer: c
Explanation: Hanging else statement.

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

  1. <?php
  2. $a = 10;
  3. if (0) 
  4.     print "all";
  5.  if 
  6.  else 
  7.      print "some"
  8. ?>

a) all
b) some
c) error
d) no output
View Answer

Answer: c
Explanation: No else statement to end the if statement.

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

  1. <?php
  2. $a = "";
  3. if ($a) 
  4.     print "all";
  5. else 
  6.     print "some";
  7. ?>

a) all
b) some
c) error
d) no output
View Answer

Answer: b
Explanation: Empty string is evaluated to 0.

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

  1. <?php
  2. $a = "a";
  3. if ($a) 
  4.     print "all";
  5. else 
  6.     print "some";
  7. ?>

a) all
b) some
c) error
d) no output
View Answer

Answer: a
Explanation: The value of a is evaluated to 1 as it has a value.

More MCQs on PHP If – Else – If:

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.