250+ TOP PHP Coding MCQs on Variables and Answers

PHP Multiple Choice Questions on “Variables – 1”.

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

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

a) 15
b) 10 + 5
c) $z
d) $x + $y
Answer: b
Clarification: Variable z will store 10 + 5 because 10 + 5 is given in double-quotes.

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

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

a) 4 = 4 + 3 + 1
b) 8
c) 8 = 4 + 3 +1
d) Error
Answer: a
Clarification: Again since the variables are inside double quotes we get this result.

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

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

a) $z
b) 15
c) 8
d) 1
Answer: c
Clarification: Normal addition of variables x, y and z occurs and result of 8 will be displayed.

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

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

a) 0
b) 1
c) 2
d) Error
Answer: b
Clarification: % 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. $x = 10;
  2. $y = 4;
  3. $z = 3;
  4. echo $x % $y % $z;
  5. ?>

a) 0
b) 1
c) 2
d) Error
Answer: c
Clarification: 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. $x = 10;
  2. $y = 4;
  3. $z = 3;
  4. echo ($x % ($y) + $z);
  5. ?>

a) 5
b) 3
c) 0
d) 1
Answer: a
Clarification: 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. $x = 30;
  2. $y = 20;
  3. $z = 10;
  4. echo $x + $y - $z / ($z - $y);
  5. ?>

a) 41
b) -4
c) -5
d) 51
Answer: d
Clarification: 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. $x = -1;
  2. $y = 1;
  3. $z = $x * $y + $z;
  4. echo $z;
  5. ?>

a) Undefined variable z
b) -1
c)

Undefined variable z
-1

d) None of the mentioned
Answer: c
Clarification: 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. $x = 4;
  2. $y = -3;
  3. $z = 11;
  4. echo 4 + $y * $z / $x;
  5. ?>

a) 4.25
b) 3.25
c) -3.25
d) -4.25
Answer: d
Clarification: 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. $x = 3.5;
  2. $y = 2;
  3. $z = 2;
  4. echo $x / $y / $z;
  5. ?>

a) 1.75
b) 0.875
c) 3.5
d) Error
Answer: b
Clarification: First $x / $y is evaluated then this is divided by $z therefore we get 0.875.

Leave a Reply

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