PHP Multiple Choice Questions – Operators

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Operators”.

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

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

a) 11111213
b) 11121213
c) 11111212
d) 11111112
View Answer

Answer: a
Explanation: ++$a increments a and then prints it, $a++ prints and then increments.
advertisement
advertisement

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

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

a) 11
b) 12
c) 10
d) error
View Answer

Answer: a
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?

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

a) testthisthisalso
b) testthis
c) testthisalsothisalso
d) error at line 4
View Answer

Answer: c
Explanation: The x .= y is a shorthand for x = x.y and this is evaluated from right to left.
advertisement

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

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

a) no output
b) true 32
c) true 23
d) true 22
View Answer

Answer: b
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?

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

a) 80.5
b) 44
c) 82
d) 42
View Answer

Answer: d
Explanation: Expression is evaluated from right to left.

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

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

a) 2
b) 1
c) 3
d) no output
View Answer

Answer: a
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?

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

a) 4
b) 2
c) error at line2
d) no output
View Answer

Answer: c
Explanation: The ** is not a valid operator, only ++ and — exist.

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

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

a) 1
b) 0
c) 2
d) no output
View Answer

Answer: b
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?

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

a) 1
b) 0
c) 2
d) undefined
View Answer

Answer: d
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?

  1. <?php
  2. $x = 0;
  3. echo $x-- != ++$x;
  4. ?>

a) 1
b) 0
c) error
d) no output
View Answer

Answer: a
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?

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

a) result is true
b) result is 1
c) error
d) no output
View Answer

Answer: b
Explanation: Result is x&&y which returns 1 if both x and y are true.

More MCQs on PHP Operators:

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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.