This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “If – Else – If”.
1. What will be the output of the following PHP code?
<?php
$x;
if ($x)
print "hi" ;
else
print "how are u";
?>
a) how are u
b) hi
c) error
d) no output
View Answer
Explanation: Uninitialized x is set to 0, thus if condition fails.
2. What will be the output of the following PHP code?
<?php
$x = 0;
if ($x++)
print "hi";
else
print "how are u";
?>
a) hi
b) no output
c) error
d) how are u
View Answer
Explanation: x is incremented after if which evaluates to false.
3. What will be the output of the following PHP code?
<?php
$x;
if ($x == 0)
print "hi" ;
else
print "how are u";
print "hello"
?>
a) how are uhello
b) hihello
c) hi
d) no output
View Answer
Explanation: else condition without brackets performs the following statements only.
4. What will be the output of the following PHP code?
<?php
$x = 0;
if ($x == 1)
if ($x >= 0)
print "true";
else
print "false";
?>
a) true
b) false
c) error
d) no output
View Answer
Explanation: The nested for loop is not entered if outer condition is false.
5. What will be the output of the following PHP code?
<?php
$a = 1;
if ($a--)
print "True";
if ($a++)
print "False";
?>
a) true
b) false
c) error
d) no output
View Answer
Explanation: Due to post increment and post decrement only the first condition is satisfied.
6. What will be the output of the following PHP code?
<?php
$a = 1;
if (echo $a)
print "True";
else
print "False";
?>
a) true
b) false
c) error
d) no output
View Answer
Explanation: echo does not return anything so if condition is empty.
7. What will be the output of the following PHP code?
<?php
$a = 1;
if (print $a)
print "True";
else
print "False";
?>
a) 1True
b) True
c) error
d) False
View Answer
Explanation: print returns 1 if it prints anything.
8. What will be the output of the following PHP code?
<?php
$a = 10;
if (1)
print "all";
else
print "some"
else
print "none";
?>
a) all
b) some
c) error
d) none
View Answer
Explanation: Hanging else statement.
9. What will be the output of the following PHP code?
<?php
$a = 10;
if (0)
print "all";
if
else
print "some"
?>
a) all
b) some
c) error
d) no output
View Answer
Explanation: No else statement to end the if statement.
10. What will be the output of the following PHP code?
<?php
$a = "";
if ($a)
print "all";
else
print "some";
?>
a) all
b) some
c) error
d) no output
View Answer
Explanation: Empty string is evaluated to 0.
11. What will be the output of the following PHP code?
<?php
$a = "a";
if ($a)
print "all";
else
print "some";
?>
a) all
b) some
c) error
d) no output
View Answer
Explanation: The value of a is evaluated to 1 as it has a value.
More MCQs on PHP If – Else – If:
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ 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]
- Practice MCA MCQs
- Check MCA Books
- Check Information Technology Books
- Apply for Programming Internship
- Practice Programming MCQs