250+ TOP PHP Coding MCQs on Variables – 3 and Answers

PHP Problems on “Variables – 3”.

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

a) 0
b) 1
c) Nothing
d) Error
Answer: c
Clarification: Since the variable x is not initialised it is not storing any value, therefore nothing will be printed on the screen.

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

  1. $x = 5;
  2. {
  3.     $x = 10;
  4.     echo "$x";
  5. }
  6. echo "$x";
  7. ?>

a) 1010
b) 105
c) 510
d) error
Answer: a
Clarification: Variable x stores the value 10 and not 5.

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

  1. $x = 5;
  2. {
  3.     echo "$x";
  4. }
  5. ?>

a) 0
b) 5
c) Nothing
d) Error
Answer: b
Clarification: The variable x stores the value 5 and therefore the value 5 is printed on the screen.

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

  1. $x = 5;
  2. function fun()
  3. {
  4.     echo "$x";
  5. }
  6. fun();
  7. ?>

a) 0
b) 5
c) Nothing
d) Error
Answer: c
Clarification: The variable x is not defined inside the function fun(), therefore nothing is printed on the screen.

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

  1. $x = 5;
  2. function fun()
  3. {
  4.     $x = 10;
  5.     echo "$x";
  6. }
  7. fun();
  8. echo "$x";
  9. ?>

a) 0
b) 105
c) 510
d) Error
Answer: b
Clarification: First when the function is called variable x is initialised to 10 so 10 is printed later the global value 5 is printed.

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

  1. $x = 4;
  2. $y = 3;
  3. function fun($x = 3, $y = 4)
  4. {
  5.     $z = $x+$y/$y+$x;
  6.     echo "$z";
  7. } 
  8. echo $x;
  9. echo $y;
  10. echo $z; 
  11. fun($x, $y);
  12. ?>

a) 43
b) 943
c) 349
d) 439
Answer: d
Clarification: Firstly, the statements outside the function are printed, since z is not defined it’ll no value is printed for z. Next the function is called and the value of z inside the function is printed.

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

  1. $x = 4;
  2. $y = 3;
  3. function fun($x, $y)
  4. {
  5.     $z = $x + $y / $y + $x;
  6.     echo "$z";
  7. }
  8. echo $x;
  9. echo $y;
  10. echo $z; 
  11. fun(3, 4);
  12. ?>

a) 437
b) 439
c) 349
d) 347
Answer: a
Clarification: It is same as above but the value passed into the function is 3,4 and not 4,3. Therefore the difference in answer.

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

  1. function fun($x,$y)
  2. {
  3.     $x = 4;
  4.     $y = 3;
  5.     $z = $x + $y / $y + $x;
  6.     echo "$z";
  7. }
  8. fun(3, 4); 
  9. ?>

a) 7
b) 9
c) 0
d) Error
Answer: b
Clarification: Value 3, 4 is passed to the function but that is lost because x and y are initialised to 4 and 3 inside the function. Therefore we get the given result.

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

  1. $x = 3, 4, 5, 6;
  2. echo "$x";
  3. ?>

a) 3
b) 4
c) 6
d) Error
Answer: d
Clarification: In C you won’t get an error but in PHP you’ll get a syntax error.

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

  1. $a = 10;
  2. $b = 4;
  3. $c = fun(10,4);
  4. function fun($a,$b)
  5. {
  6.     $b = 3;
  7.     return $a - $b + $b - $a; 
  8. }
  9. echo $a;
  10. echo $b;
  11. echo $c;
  12. ?>

a) 104
b) 410
c) 1400
d) 4100
Answer: c
Clarification: The value returned from the function is 0, and value of a is 10, value of b is 4 and c is 0.

PHP Problems,

Leave a Reply

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