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

PHP Multiple Choice Questions on “If – Else – If – 1”.

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

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

a) how are u
b) hi
c) error
d) no output
Answer: a
Clarification: Uninitialized x is set to 0, thus if condition fails.

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

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

a) hi
b) no output
c) error
d) how are u
Answer: d
Clarification: x is incremented after if which evaluates to false.

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

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

a) how are uhello
b) hihello
c) hi
d) no output
Answer: b
Clarification: else condition without brackets performs the following statements only.

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

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

a) true
b) false
c) error
d) no output
Answer: d
Clarification: The nested for loop is not entered if outer condition is false.

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

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

a) true
b) false
c) error
d) no output
Answer: a
Clarification: Due to post increment and post decrement only the first condition is satisfied.

  250+ TOP MCQs on PHP Object Tools and Answers

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

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

a) true
b) false
c) error
d) no output
Answer: c
Clarification: echo does not return anything so if condition is empty.

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

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

a) true
b) false
c) error
d) no output
Answer: a
Clarification: print returns 1 if it prints anything.

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

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

a) all
b) some
c) error
d) none
Answer: c
Clarification: Hanging else statement.

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

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

a) all
b) some
c) error
d) no output
Answer: c
Clarification: No else statement to end the if statement.

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

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

a) all
b) some
c) error
d) no output
Answer: b
Clarification: Empty string is evaluated to 0.

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

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

a) all
b) some
c) error
d) no output
Answer: a
Clarification: The value of a is evaluated to 1 as it has a value.

  250+ TOP PHP Coding MCQs on Functions and Answers

Leave a Comment

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

Scroll to Top