PHP Coding Questions and Answers – Operators – 4

This set of PHP Questions and Answers focuses on “Operators – 4”.

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

  1. <?php
  2. echo 5 * 9 / 3 + 9;
  3. ?>

a) 24
b) 3.7
c) 3.85
d) 0
View Answer

Answer: a
Explanation: Operator precedence order must be followed.
advertisement
advertisement

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

  1. <?php
  2. $var1 = 1 + ++5;
  3. echo $var1; 
  4. ?>

a) no output
b) error
c) 6
d) 7
View Answer

Answer: b
Explanation: Operator ++ can be done only on variables.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

advertisement
  1. <?php
  2. $var1 = 0;
  3. $var1 = ($var1 + 5)++; 
  4. echo $var1; 
  5. ?>

a) 5
b) error
c) 6
d) 7
View Answer

Answer: b
Explanation: Operator ++ can be done only on variables.
advertisement

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

  1. <?php
  2. $var1 = 0;
  3. $var1 = $var1++ + 5; 
  4. echo $var1; 
  5. ?>

a) 5
b) error
c) 6
d) 7
View Answer

Answer: a
Explanation: Operator precedence followed.

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

  1. <?php
  2. $var1 = 0;
  3. $var1 = ++$var1 + 5; 
  4. echo $var1; 
  5. ?>

a) 5
b) error
c) 6
d) 7
View Answer

Answer: c
Explanation: Operator precedence followed.

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

  1. <?php
  2. $var1 = 0;
  3. $var1 = $var1 + 5; 
  4. echo $var1++; 
  5. ?>

a) 5
b) error
c) 6
d) 7
View Answer

Answer: a
Explanation: Operator precedence followed,incremented after display.

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

  1. <?php
  2. $var1 = 1;
  3. echo $var1 = ++$var1 % 2 + ++$var1; 
  4. ?>

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

Answer: d
Explanation: Evaluation done from right to left.

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

  1. <?php
  2.  $a = 5;$b = -7;$c =0; 
  3.  $d = ++$a && ++$b || ++$c;
  4.  print $d;print $a;
  5. ?>

a) 16
b) 06
c) 15
d) 05
View Answer

Answer: a
Explanation: 1&&0||1 is evaluated to 1 and the a is also pre incremented to 6.

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

  1. <?php
  2. $var1 = 3;
  3. print $var = ++$var;
  4. ?>

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

Answer: a
Explanation: $var = ++$var returns 1(success).

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

  1. <?php
  2. $var1 = 3;
  3. print ++$var++;
  4. ?>

a) 3
b) 4
c) 5
d) error
View Answer

Answer: d
Explanation: First pre increment is done and the result is a number, thus post increment cannot be performed on it.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of PHP, 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.