PHP Multiple Choice Questions on “If – Else – If – 1”.
1. What will be the output of the following PHP code?
-
-
$x;
-
if ($x)
-
print "hi" ;
-
else -
print "how are u";
-
?>
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?
-
-
$x = 0;
-
if ($x++)
-
print "hi";
-
else -
print "how are u";
-
?>
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?
-
-
$x;
-
if ($x == 0)
-
print "hi" ;
-
else -
print "how are u";
-
print "hello"
-
?>
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?
-
-
$x = 0;
-
if ($x == 1)
-
if ($x >= 0)
-
print "true";
-
else -
print "false";
-
?>
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?
-
-
$a = 1;
-
if ($a--)
-
print "True";
-
if ($a++)
-
print "False";
-
?>
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.
6. What will be the output of the following PHP code?
-
-
$a = 1;
-
if (echo $a)
-
print "True";
-
else -
print "False";
-
?>
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?
-
-
$a = 1;
-
if (print $a)
-
print "True";
-
else -
print "False";
-
?>
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?
-
-
$a = 10;
-
if (1)
-
print "all";
-
else -
print "some"
-
else -
print "none";
-
?>
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?
-
-
$a = 10;
-
if (0)
-
print "all";
-
if -
else -
print "some"
-
?>
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?
-
-
$a = "";
-
if ($a)
-
print "all";
-
if -
else -
print "some";
-
?>
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?
-
-
$a = "a";
-
if ($a)
-
print "all";
-
else -
print "some";
-
?>
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.
