PHP Multiple Choice Questions on “Operators – 1”.
1. What will be the output of the following PHP code?
-
-
$a = 10;
-
echo ++$a;
-
echo $a++;
-
echo $a;
-
echo ++$a;
-
?>
a) 11111213
b) 11121213
c) 11111212
d) 11111112
Answer: a
Clarification: ++$a increments a and then prints it, $a++ prints and then increments.
2. What will be the output of the following PHP code?
-
-
$a = 12;
-
--$a;
-
echo $a++;
-
?>
a) 11
b) 12
c) 10
d) error
Answer: a
Clarification: 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?
-
-
$x = "test";
-
$y = "this";
-
$z = "also";
-
$x .= $y .= $z ;
-
echo $x;
-
echo $y;
-
?>
a) testthisthisalso
b) testthis
c) testthisalsothisalso
d) error at line 4
Answer: c
Clarification: 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?
-
-
$x = 1;
-
$y = 2;
-
if (++$x == $y++)
-
{ -
echo "true ", $y, $x;
-
} -
?>
a) no output
b) true 23
c) true 22
d) true 33
Answer: b
Clarification: 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?
-
-
$y = 2;
-
$w = 4;
-
$y *= $w /= $y;
-
echo $y, $w;
-
?>
a) 80.5
b) 44
c) 82
d) 42
Answer: d
Clarification: Expression is evaluated from right to left.
6. What will be the output of the following PHP code?
-
-
$y = 2;
-
if ($y-- == ++$y)
-
{ -
echo $y;
-
} -
?>
a) 2
b) 1
c) 3
d) no output
Answer: a
Clarification: 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?
-
-
$y = 2;
-
if (**$y == 4)
-
{ -
echo $y;
-
} -
?>
a) 4
b) 2
c) error at line2
d) no output
Answer: c
Clarification: The ** is not a valid operator, only ++ and — exist.
8. What will be the output of the following PHP code?
-
-
$y = 2;
-
if (--$y == 2 || $y xor --$y)
-
{ -
echo $y;
-
} -
?>
a) 1
b) 0
c) 2
d) no output
Answer: b
Clarification: –$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?
-
-
$y = 2;
-
if (--$y <> ($y != $y++))
-
{ -
echo $y;
-
} -
?>
a) 1
b) 0
c) 2
d) no output
Answer: b
Clarification: –$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.
10. What will be the output of the following PHP code?
-
-
echo $x-- != ++$x;
-
?>
a) 1
b) 0
c) error
d) no output
Answer: a
Clarification: Automatically 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?
-
-
$auth = 1;
-
$status = 1;
-
if ($result = (($auth == 1) && ($status != 0)))
-
{ -
print "result is $result
"; -
} -
?>
a) result is true
b) result is 1
c) error
d) no output
Answer: b
Clarification: Result is x&&y which returns 1 if both x and y are true.
