PHP Multiple Choice Questions – Variables

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

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

  1. <?php
  2. $x = 5;
  3. $y = 10;
  4. $z = "$x + $y";
  5. echo "$z";
  6. ?>

a) 15
b) 10 + 5
c) $z
d) $x + $y
View Answer

Answer: b
Explanation: Variable z will store 10 + 5 because 10 + 5 is given in double-quotes.
advertisement
advertisement

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

  1. <?php
  2. $x = 4;
  3. $y = 3;
  4. $z = 1;
  5. echo "$x = $x + $y + $z";
  6. ?>

a) 4 = 4 + 3 + 1
b) 8
c) 8 = 4 + 3 +1
d) Error
View Answer

Answer: a
Explanation: Again since the variables are inside double quotes we get this result.

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

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

a) $z
b) 15
c) 8
d) 1
View Answer

Answer: c
Explanation: Normal addition of variables x, y and z occurs and result of 8 will be displayed.
advertisement

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

  1. <?php
  2. $x = 3.3;
  3. $y = 2;
  4. echo $x % $y;
  5. ?>

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

Answer: b
Explanation: % is the modulo operator. Unlike in C we can use it get reminder or floating point numbers in PHP.

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

  1. <?php
  2. $x = 10;
  3. $y = 4;
  4. $z = 3;
  5. echo $x % $y % $z;
  6. ?>

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

Answer: c
Explanation: The expression is considered as ($x%$y)%z in this case (10%4)%3 which is 2.

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

  1. <?php
  2. $x = 10;
  3. $y = 4;
  4. $z = 3;
  5. echo ($x % ($y) + $z);
  6. ?>

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

Answer: a
Explanation: The innermost bracket is evaluated first, since it covers only variable y it is as good as not using brackets.

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

  1. <?php
  2. $x = 30;
  3. $y = 20;
  4. $z = 10;
  5. echo $x + $y - $z / ($z - $y);
  6. ?>

a) 41
b) -4
c) -5
d) 51
View Answer

Answer: d
Explanation: First ($z – $y) is evaluated then -$z/($z – $y) is evaluated this results in 1 which is added to $x + $y therefore we get 51.

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

  1. <?php
  2. $x = -1;
  3. $y = 1;
  4. $z = $x * $y + $z;
  5. echo $z;
  6. ?>

a) Undefined variable z
b) -1
c)

Undefined variable z
-1

d) None of the mentioned
View Answer

Answer: c
Explanation: Since the variable z is not defined it returns the error also it takes z as 0 and returns the value -1.

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

  1. <?php
  2. $x = 4;
  3. $y = -3;
  4. $z = 11;
  5. echo 4 + $y * $z / $x;
  6. ?>

a) 4.25
b) 3.25
c) -3.25
d) -4.25
View Answer

Answer: d
Explanation: First the * is evaluated then / followed by + therefore we can rewrite this expression as 4 +((- 3 * 11) / 4) which results in -4.25.

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

  1. <?php
  2. $x = 3.5;
  3. $y = 2;
  4. $z = 2;
  5. echo $x / $y / $z;
  6. ?>

a) 1.75
b) 0.875
c) 3.5
d) Error
View Answer

Answer: b
Explanation: First $x / $y is evaluated then this is divided by $z therefore we get 0.875.

More MCQs on PHP Variables:

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.