PHP Coding Questions and Answers – Variables – 4

This set of Basic PHP Questions and Answers focuses on “Variables – 4”.

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

  1. <?php
  2. $a = "$winner";
  3. $b = "/$looser";
  4. echo $a,$b;
  5. ?>

a) $winner/$looser
b) /$looser
c) /
d) $looser
View Answer

Answer: c
Explanation: Since variables $winner and $looser is not defined we only see / as output.
advertisement
advertisement

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

  1. <?php
  2. $a = "$winner";
  3. $b = "\$looser";
  4. echo $a, $b;
  5. ?>

a) $winner\$looser
b) \$looser
c) \
d) $looser
View Answer

Answer: d
Explanation: As there is a backslash before $ it takes it as a string and not a variable therefore we get $looser as the output.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

advertisement
  1. <?php
  2. $a = "$winner";
  3. $b = "\\$looser";
  4. echo $a, $b;
  5. ?>

a) $winner\\$looser
b) \\$looser
c) \
d) $looser
View Answer

Answer: c
Explanation: Since two backslashes are used together, a single backslash is printed on the screen and as $looser is not initialised only single backslash is printed.
advertisement

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

  1. <?php
  2. $x = 5;
  3. $y = 10;
  4. function fun()
  5. {
  6.     $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
  7. } 
  8. fun();
  9. echo $y;
  10. ?>

a) 5
b) 10
c) 15
d) Error
View Answer

Answer: c
Explanation: You can access the global variable using $GLOBALS[‘globalvariablename’].

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

  1. <?php
  2. $x = 5;
  3. $y = 10;
  4. function fun()
  5. {
  6.     $y = $GLOBALS['x'] + $GLOBALS['y'];
  7. } 
  8. fun();
  9. echo $y;
  10. ?>

a) 5
b) 10
c) 15
d) Error
View Answer

Answer: b
Explanation: The value of global variable y does not change therefore it’ll print 10;

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

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

a) 012
b) 123
c) 000
d) 111
View Answer

Answer: c
Explanation: Every time the function is called the value of x becomes 0, therefore we get 0 on every function call.

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

  1. <?php
  2. function fun()
  3. {
  4.     static $x = 0;
  5.     echo $x;
  6.     $x++;
  7. }
  8. fun();
  9. fun();
  10. fun();
  11. ?>

a) 012
b) 123
c) 111
d) Error
View Answer

Answer: a
Explanation: When static is used, each time the function is called, that variable will still have the information it contained from the last time the function was called.

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

  1. <?php
  2. static $x = 0;
  3. function fun()
  4. {
  5.     echo $x;
  6.     $x++;
  7. }
  8. fun();
  9. fun();
  10. fun();
  11. ?>

a) 012
b) 123
c) Nothing
d) Error
View Answer

Answer: c
Explanation: Since variable x is not defined inside the function fun(), nothing will be printed.

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

  1. <?php
  2. $x=0;
  3. function fun()
  4. {
  5.     echo $GLOBALS['x'];
  6.     $GLOBALS['x']++;
  7. }
  8. fun();
  9. fun();
  10. fun();
  11. ?>

a) 000
b) 012
c) 123
d) Error
View Answer

Answer: b
Explanation: Since, we are using $GLOBALS[‘x’].

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

  1. <?php
  2. $x = 0;
  3. function fun()
  4. {
  5.     echo $GLOBALS['x'];
  6.     $x++;
  7. }
  8. fun();
  9. fun();
  10. fun();
  11. ?>

a) 000
b) 012
c) Nothing
d) Error
View Answer

Answer: a
Explanation: Every time the function is called the value of x becomes 0, therefore we get 0 on every function call.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice basic questions and answers on all areas of PHP, 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.