250+ TOP PHP Coding MCQs on If-Else-If and Answers

PHP Puzzles on “If-Else-If – 3”.

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

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

a) hi
b) hello
c) error
d) no output
Answer: a
Clarification: In php division returns a float that is a non zero value thus evaluates to true.

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

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

a) hi
b) hihellohi
c) hihi
d) no output
Answer: a
Clarification: Print returns true and thus the first if statement also is not executed.

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

  1. if (print "hi" - 1)
  2.     print "hello"
  3. ?>

a) hi
b) hihello
c) error
d) no output
Answer: c
Clarification: print returns true and when 1 is subtracted it is syntax error.

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

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

a) hi
b) hello
c) error
d) no output
Answer: c
Clarification: 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. $a = 10;
  2. $b = 11;
  3. if ($a < ++$a || $b < ++$b)
  4.     print "hello";
  5. else
  6.     print "hi";
  7. ?>

a) hi
b) hello
c) error
d) no output
Answer: a
Clarification: The operator precedence of ++ is higher than <, thus the increment happens first and then compared.

  250+ TOP PHP Coding MCQs on Arrays and Answers

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

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

a) hi
b) hello
c) error
d) no output
Answer: b
Clarification: 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. $a = 2;
  2. if ($a-- - --$a - $a)
  3.     print "hello";
  4. else
  5.     print "hi";
  6. ?>

a) hi
b) hello
c) error
d) no output
Answer: b
Clarification: 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. $a = "hello";
  2. if ($a.length)
  3.     print $a.length;
  4. else
  5.     print "hi";
  6. ?>

a) hellolength
b) 5
c) hi
d) error
Answer: a
Clarification: The . operator appends a string and returns true.

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

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

a) hellolength
b) 5
c) hi
d) error
Answer: b
Clarification: 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. $a = "1";
  2. $b = "0";
  3. if ((int)$a && $b) 
  4.     print"hi";
  5. else 
  6.     print "hello";
  7. ?>

a) hello
b) no output
c) hi
d) error
Answer: a
Clarification: The expression is evaluated with values contained in the string, even without typecasting.

  250+ TOP PHP Coding MCQs on Switch and Answers

To practice all Puzzles on PHP, here is complete set of 250+ Multiple Choice Questions and Answers on PHP.

Leave a Comment

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

Scroll to Top