This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Operators”.
1. What will be the output of the following PHP code?
<?php
$a = 10;
echo ++$a;
echo $a++;
echo $a;
echo ++$a;
?>
a) 11111213
b) 11121213
c) 11111212
d) 11111112
View Answer
Explanation: ++$a increments a and then prints it, $a++ prints and then increments.
2. What will be the output of the following PHP code?
<?php
$a = 12;
--$a;
echo $a++;
?>
a) 11
b) 12
c) 10
d) error
View Answer
Explanation: The + operator does union of arrays in that order, then the === operator compares key and value pairs.
3. What will be the output of the following PHP code?
<?php
$x = "test";
$y = "this";
$z = "also";
$x .= $y .= $z ;
echo $x;
echo $y;
?>
a) testthisthisalso
b) testthis
c) testthisalsothisalso
d) error at line 4
View Answer
Explanation: The x .= y is a shorthand for x = x.y and this is evaluated from right to left.
4. What will be the output of the following PHP code?
<?php
$x = 1;
$y = 2;
if (++$x == $y++)
{
echo "true ", $y, $x;
}
?>
a) no output
b) true 32
c) true 23
d) true 22
View Answer
Explanation: x is preincremented and y is post incremented thus both are 2 in the if condition, later y is increment.
5. What will be the output of the following PHP code?
<?php
$y = 2;
$w = 4;
$y *= $w /= $y;
echo $y, $w;
?>
a) 80.5
b) 44
c) 82
d) 42
View Answer
Explanation: Expression is evaluated from right to left.
6. What will be the output of the following PHP code?
<?php
$y = 2;
if ($y-- == ++$y)
{
echo $y;
}
?>
a) 2
b) 1
c) 3
d) no output
View Answer
Explanation: First $y = 2 is compared to and then decremented, then incremented and compared to $y = 2.
7. What will be the output of the following PHP code?
<?php
$y = 2;
if (**$y == 4)
{
echo $y;
}
?>
a) 4
b) 2
c) error at line2
d) no output
View Answer
Explanation: The ** is not a valid operator, only ++ and — exist.
8. What will be the output of the following PHP code?
<?php
$y = 2;
if (--$y == 2 || $y xor --$y)
{
echo $y;
}
?>
a) 1
b) 0
c) 2
d) no output
View Answer
Explanation: –$y == 2 is false but y is decremented, the xor gives true if only one of the operands are true, thus 1 xor 0 is true.
9. What will be the output of the following PHP code?
<?php
$y = 2;
if (--$y <> ($y != $y++))
{
echo $y;
}
?>
a) 1
b) 0
c) 2
d) undefined
View Answer
Explanation: If we traverse this expression from left to right, the answer seems to be 2. However, php is evaluating the expression from right to left, i.e., ($y != $y++) is evaluation first. The result of this comparison (3 != 2) will be “true” and this will be compared for “not-equal-to” with –$y, and the result will false and hence “no output”. So, the final results depends on the order of evaluation. So, “undefined” is the correct answer.
Note: As per PHP specification, Operator precedence and associativity only determine how expressions are grouped, they do not specify an order of evaluation. PHP does not (in the general case) specify in which order an expression is evaluated and code that assumes a specific order of evaluation should be avoided, because the behavior can change between versions of PHP or depending on the surrounding code.
10. What will be the output of the following PHP code?
<?php
$x = 0;
echo $x-- != ++$x;
?>
a) 1
b) 0
c) error
d) no output
View Answer
Explanation: x is declared and initialized to 0, then decremented and compared with its increments, thus returns 1.
11. What will be the output of the following PHP code?
<?php
$auth = 1;
$status = 1;
if ($result = (($auth == 1) && ($status != 0)))
{
print "result is $result<br />";
}
?>
a) result is true
b) result is 1
c) error
d) no output
View Answer
Explanation: Result is x&&y which returns 1 if both x and y are true.
More MCQs on PHP Operators:
- PHP Operators MCQ (Set 2)
- PHP Operators MCQ (Set 3)
- PHP Operators MCQ (Set 4)
- PHP Operators MCQ (Set 5)
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all areas of PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check MCA Books
- Practice MCA MCQs
- Practice Programming MCQs
- Check Information Technology Books
- Check PHP Books