PHP Coding Questions and Answers – If-Else-If-2

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

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

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

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

Answer: b
Explanation: Expression evaluates to true.
advertisement
advertisement

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

  1. <?php
  2. $x = 10;
  3. $y = 20;
  4. if ($x > $y && 1||1)
  5.     print "hi" ;
  6. else
  7.     print "how are u";
  8. ?>

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

Answer: b
Explanation: Expression evaluates to true.
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 = 10;
  3. $y = 20;
  4. if ($x > $y && 1||1)
  5.     print "hi" ;
  6. else
  7.     print "how are u";
  8. ?>

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

Answer: b
Explanation: Expression evaluates to true.
advertisement

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

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

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

Answer: b
Explanation: Expression evaluates to true.

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

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

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

Answer: b
Explanation: Expression evaluates to true.

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

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

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

Answer: a
Explanation: Expression evaluates to false.

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

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

a) 0how are u
b) 0hi
c) hi
d) how are u
View Answer

Answer: b
Explanation: Expression evaluates to true as print returns 1.

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

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

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

Answer: b
Explanation: Enters if else as first condition is false and thus x is set to 2.

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

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

a) 0
b) 1
c) error
d) how are u
View Answer

Answer: d
Explanation: x&0 is 0,thus evaluated to false.

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

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

a) 0
b) 1
c) error
d) how are u
View Answer

Answer: d
Explanation: x&0 is 0,thus evaluated to false.

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

  1. <?php
  2. $x = 1;
  3. if ($x = $x&0)
  4.     print $x;
  5. else
  6.     break;
  7. ?>

a) 0
b) 1
c) error
d) no output
View Answer

Answer: c
Explanation: break is not defined for a if else ladder.

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

  1. <?php
  2. $a = 100;
  3. if ($a > 10)
  4.     printf("M.S. Dhoni");
  5. else if ($a > 20)
  6.     printf("M.E.K Hussey");
  7. else if($a > 30)
  8.     printf("A.B. de villiers");
  9. ?>

a) M.S.Dhoni
b) M.E.K.Hussey
c)

M.S.Dhoni
M.E.K.Hussey
A.B.de villiers

d) No output
View Answer

Answer: a
Explanation: In if else if one condition is satisfied then no other condition is checked.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

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.