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

This set of PHP Puzzles focuses on “If-Else-If – 3”.

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

  1. <?php
  2. $x = 10;
  3. $y = 5;
  4. $z = 3;
  5. if ($x / $y / $z)
  6.     print "hi";
  7. else
  8.     print "hello";
  9. ?>

a) hi
b) hello
c) error
d) no output
View Answer

Answer: a
Explanation: In php division returns a float that is a non zero value thus evaluates to true.
advertisement
advertisement

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

  1. <?php
  2. if (!print "hi")
  3.     if (print "hello")
  4. print "hi";
  5. ?>

a) hi
b) hihellohi
c) hihi
d) no output
View Answer

Answer: a
Explanation: Print returns true and thus the first if statement also is not executed.
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. if (print "hi" - 1)
  3.     print "hello"
  4. ?>

a) hi
b) hihello
c) error
d) no output
View Answer

Answer: c
Explanation: print returns true and when 1 is subtracted it is syntax error.
advertisement

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

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

a) hi
b) hello
c) error
d) no output
View Answer

Answer: c
Explanation: The if statement has no brackets and it expects a else/else if after a if.

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

  1. <?php
  2. $a = 10;
  3. $b = 11;
  4. if ($a < ++$a || $b < ++$b)
  5.     print "hello";
  6. else
  7.     print "hi";
  8. ?>

a) hi
b) hello
c) error
d) no output
View Answer

Answer: a
Explanation: The operator precedence of ++ is higher than <, thus the increment happens first and then compared.

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

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

a) hi
b) hello
c) error
d) no output
View Answer

Answer: b
Explanation: Computing the expression in the if clause,it sums upto to 2 which is a positive value.

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

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

a) hi
b) hello
c) error
d) no output
View Answer

Answer: b
Explanation: Computing the expression in the if clause, it sums upto to 2 which is a positive value.

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

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

a) hellolength
b) 5
c) hi
d) error
View Answer

Answer: a
Explanation: The . operator appends the string, the final string will be hellolength. Since the string is non-empty, the if condition returns true.

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

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

a) hellolength
b) 5
c) hi
d) error
View Answer

Answer: b
Explanation: The function strlen($a) gives the length of the string, 5, which is considered true.

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

  1. <?php
  2. $a = "1";
  3. $b = "0";
  4. if ((int)$a && $b) 
  5.     print"hi";
  6. else 
  7.     print "hello";
  8. ?>

a) hello
b) no output
c) hi
d) error
View Answer

Answer: a
Explanation: The expression is evaluated with values contained in the string, even without typecasting.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all Puzzles on PHP, here is complete set of 250+ 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.