250+ TOP PHP Coding MCQs on Operators and Answers

PHP Multiple Choice Questions on “Operators – 1”.

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

  1. $a = 10;
  2. echo ++$a;
  3. echo $a++;
  4. echo $a;
  5. echo ++$a;
  6. ?>

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?

  1. $a = 12;
  2. --$a;
  3. echo $a++;
  4. ?>

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?

  1. $x = "test";
  2. $y = "this";
  3. $z = "also"; 
  4. $x .= $y .= $z ;
  5. echo $x;
  6. echo $y;
  7. ?>

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?

  1. $x = 1;
  2. $y = 2;
  3. if (++$x == $y++)
  4. {
  5.     echo "true ", $y, $x;
  6. }
  7. ?>

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?

  1. $y = 2;
  2. $w = 4;
  3. $y *= $w /= $y;
  4. echo $y, $w;
  5. ?>

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?

  1. $y = 2;
  2. if ($y-- == ++$y)
  3. {
  4.     echo $y;
  5. }
  6. ?>

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?

  1. $y = 2;
  2. if (**$y == 4)
  3. {
  4.     echo $y;
  5. }
  6. ?>

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?

  1. $y = 2;
  2. if (--$y == 2 || $y xor --$y)
  3. {
  4.     echo $y;
  5. }
  6. ?>

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?

  1. $y = 2;
  2. if (--$y <> ($y != $y++))
  3. {
  4.     echo $y;
  5. }
  6. ?>

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?

  1. echo $x-- != ++$x;
  2. ?>

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.

  250+ TOP PHP Coding MCQs on Arrays and Answers

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

  1. $auth = 1;
  2. $status = 1;
  3. if ($result = (($auth == 1) && ($status != 0)))
  4. {
  5.     print "result is $result
    "
    ;
  6. }
  7. ?>

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.

Leave a Comment

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

Scroll to Top