PHP Coding Questions and Answers – Variables – 2

This set of PHP Technical Interview Questions & Answers focuses on “Variables – 2”.

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

  1. <?php
  2. one = 1;
  3. two = 2;
  4. three = 3;
  5. four = 4;
  6. echo "one / two + three / four";
  7. ?>

a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer

Answer: d
Explanation: Variables should start with a $ symbol, since one, two, three, four don’t begin with $ symbol we’ll get an error.
advertisement
advertisement

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

  1. <?php
  2. $on$e = 1;
  3. $tw$o = 2;
  4. $thre$e = 3;
  5. $fou$r = 4;
  6. echo "$on$e / $tw$o + $thre$e / $fou$r"; 
  7. ?>

a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer

Answer: d
Explanation: You can not use the $ in between the variable name.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

advertisement
  1. <?php
  2. $on_e = 1;
  3. $tw_o = 2;
  4. $thre_e = 3;
  5. $fou_r = 4;
  6. echo $on_e / $tw_o + $thre_e / $fou_r; 
  7. ?>

a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer

Answer: c
Explanation: You can use _ in a variable name.
advertisement

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

  1. <?php
  2. $On_e = 1;
  3. $tw_o = 2;
  4. $thre_e = 3;
  5. $fou_r = 4;
  6. echo $on_e / $tw_o + $thre_e / $fou_r;
  7. ?>

a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer

Answer: a
Explanation: Since the variable initialised is $On_e and the variable in the echo statement is $on_e the echo statement treats $on_e as 0;

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

  1. <?php
  2. echo $red;
  3. ?>

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

Answer: b
Explanation: There will no output returned as the variable $red does not hold any value.

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

  1. <?php
  2. $four4 = 4;
  3. $three3 = 3;
  4. $two2 = 2;
  5. echo $four4 + $three3 / $two2 - 1;
  6. ?>

a) 4.5
b) 7
c) 3.5
d) Error
View Answer

Answer: a
Explanation: You can use numbers in a variable name.

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

  1. <?php
  2. $4four = 4;
  3. $3three = 3;
  4. $2two = 2;
  5. echo $4four + $3three / $2two - 1;
  6. ?>

a) 4.5
b) 7
c) 3.5
d) Error
View Answer

Answer: d
Explanation: A variable name can not start with a numeric value.

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

  1. <?php
  2. int $one = 1;
  3. echo "$one";
  4. ?>

a) 0
b) 1
c) $one
d) Error
View Answer

Answer: d
Explanation: Unlike other programming languages there are no data types in PHP.

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

  1. <?php
  2. var $one = 1;
  3. var $two = 2;
  4. echo $one / $two * $one / $two * $two;
  5. ?>

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

Answer: d
Explanation: You can not use var before a variable name.

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

  1. <?php
  2. $hello = "Hello World";
  3. $bye = "Bye";
  4. echo $hello;"$bye";
  5. ?>

a) Hello World
b) Bye
c) Hello worldBye
d) Error
View Answer

Answer: a
Explanation: Since there is a semi-colon in between $hello and $bye, the line ends at $hello. However $bye would have printed if a echo was present before “$bye”.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all technical interview questions on 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.