PHP Coding Questions and Answers – Operators – 3

This set of PHP Questions and Answers for Entrance exams focuses on “Operators – 3”.

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. 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.

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

advertisement
  1. <?php
  2. $i = 0;
  3. $j = 0;
  4. if ($i && ($j = $i + 10)) {
  5.     echo "true";
  6. }
  7. echo $j;
  8. ?>

a) 10
b) 0
c) true0
d) true10
View Answer

Answer: b
Explanation: In if condition when the first case is 0 and is an && operation then the second command is not executed.
advertisement

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

  1. <?php
  2. $i = 10;
  3. $j = 0;
  4. if ($i || ($j = $i + 10)) {
  5.     echo "true";
  6. }
  7. echo $j;
  8. ?>

a) 20
b) true0
c) 0
d) true20
View Answer

Answer: b
Explanation: In if condition when the first case is 1 and is an || operation then the second command is not executed.

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

  1. <?php
  2. $i = 1;
  3. if ($i++ && ($i == 1))
  4.     printf("Yes\n$i");
  5. else
  6.     printf("No\n$i");
  7. ?>

a) No 2
b) Yes 1
c) Yes 2
d) No 1
View Answer

Answer: a
Explanation: The first condition returns true and increments but the second condition is false.

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

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

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

Answer: a
Explanation: Post increment of a is done after expression evaluation.

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

  1. <?php
  2. $a = 1; $b = 1; $d = 1;
  3. print ++$a + ++$a+$a++; print $a++ + ++$b; print ++$d + $d++ + $a++;
  4. ?>

a) 869
b) 742
c) 368
d) error
View Answer

Answer: a
Explanation: Follow the order of post and pre increments.

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

  1. <?php
  2. $a = 10; $b = 10;
  3. if ($a = 5)
  4.     $b--;
  5. print $a;print $b--;
  6. ?>

a) 58
b) 59
c) 109
d) 108
View Answer

Answer: b
Explanation: a is set to 5 in the if condition and b is post decremented in the print statement.

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

  1. <?php
  2. $i = 0;
  3. $x = $i++; $y = ++$i;
  4. print $x; print $y; 
  5. ?>

a) 02
b) 12
c) 01
d) 21
View Answer

Answer: a
Explanation: First case i is incremented after setting x to i.

10. 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 preincremented to 6.

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

  1. <?php
  2. $b = 1; $c = 4; $a = 5; 
  3. $d = $b + $c == $a;
  4. print $d;
  5. ?>

a) 5
b) 0
c) 10
d) 1
View Answer

Answer: d
Explanation: First b and c are added and then tested if d=5, which is true thus return 1.

Sanfoundry Global Education & Learning Series – PHP Programming.

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