PHP Coding Questions and Answers – Variables – 3

This set of PHP Problems focuses on “Variables – 3”.

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

  1. <?php
  2. $x;
  3. echo "$x";
  4. ?>

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

Answer: c
Explanation: Since the variable x is not initialised it is not storing any value, therefore nothing will be printed on the screen.
advertisement
advertisement

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

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

a) 1010
b) 105
c) 510
d) error
View Answer

Answer: a
Explanation: Variable x stores the value 10 and not 5.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

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

a) 0
b) 5
c) Nothing
d) Error
View Answer

Answer: b
Explanation: The variable x stores the value 5 and therefore the value 5 is printed on the screen.
advertisement

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

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

a) 0
b) 5
c) Nothing
d) Error
View Answer

Answer: c
Explanation: 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. <?php
  2. $x = 5;
  3. function fun()
  4. {
  5.     $x = 10;
  6.     echo "$x";
  7. }
  8. fun();
  9. echo "$x";
  10. ?>

a) 0
b) 105
c) 510
d) Error
View Answer

Answer: b
Explanation: 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. <?php
  2. $x = 4;
  3. $y = 3;
  4. function fun($x = 3, $y = 4)
  5. {
  6.     $z = $x+$y/$y+$x;
  7.     echo "$z";
  8. } 
  9. echo $x;
  10. echo $y;
  11. echo $z; 
  12. fun($x, $y);
  13. ?>

a) 43
b) 943
c) 349
d) 439
View Answer

Answer: d
Explanation: 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. <?php
  2. $x = 4;
  3. $y = 3;
  4. function fun($x, $y)
  5. {
  6.     $z = $x + $y / $y + $x;
  7.     echo "$z";
  8. }
  9. echo $x;
  10. echo $y;
  11. echo $z; 
  12. fun(3, 4);
  13. ?>

a) 437
b) 439
c) 349
d) 347
View Answer

Answer: a
Explanation: 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. <?php
  2. function fun($x,$y)
  3. {
  4.     $x = 4;
  5.     $y = 3;
  6.     $z = $x + $y / $y + $x;
  7.     echo "$z";
  8. }
  9. fun(3, 4); 
  10. ?>

a) 7
b) 9
c) 0
d) Error
View Answer

Answer: b
Explanation: 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. <?php
  2. $x = 3, 4, 5, 6;
  3. echo "$x";
  4. ?>

a) 3
b) 4
c) 6
d) Error
View Answer

Answer: d
Explanation: 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. <?php
  2. $a = 10;
  3. $b = 4;
  4. $c = fun(10,4);
  5. function fun($a,$b)
  6. {
  7.     $b = 3;
  8.     return $a - $b + $b - $a; 
  9. }
  10. echo $a;
  11. echo $b;
  12. echo $c;
  13. ?>

a) 1040
b) 1004
c) 1043
d) 4100
View Answer

Answer: a
Explanation: The value returned from the function is 0, and value of a is 10, value of b is 4 and c is 0.

Sanfoundry Global Education & Learning Series – PHP Programming.

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