250+ TOP PHP Coding MCQs on Variables and Answers

PHP Technical Interview Questions & Answers on “Variables – 2”.

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

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

a) 0.75
b) 0.05
c) 1.25
d) Error
Answer: d
Clarification: Variables should start with a $ symbol, since one, two, three, four don’t begin with $ symbol we’ll get an error.

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

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

a) 0.75
b) 0.05
c) 1.25
d) Error
Answer: d
Clarification: You can not use the $ in between the variable name.

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

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

a) 0.75
b) 0.05
c) 1.25
d) Error
Answer: c
Clarification: You can use _ in a variable name.

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

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

a) 0.75
b) 0.05
c) 1.25
d) Error
Answer: a
Clarification: 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?

a) 0
b) Nothing
c) True
d) Error
Answer: b
Clarification: 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. $four4 = 4;
  2. $three3 = 3;
  3. $two2 = 2;
  4. echo $four4 + $three3 / $two2 - 1;
  5. ?>

a) 4.5
b) 7
c) 3.5
d) Error
Answer: a
Clarification: You can use numbers in a variable name.

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

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

a) 4.5
b) 7
c) 3.5
d) Error
Answer: d
Clarification: A variable name can not start with a numeric value.

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

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

a) 0
b) 1
c) $one
d) Error
Answer: d
Clarification: Unlike other programming languages there are no data types in PHP.

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

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

a) 1
b) 0
c) 0.5
d) Error
Answer: d
Clarification: You can not use var before a variable name.

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

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

a) Hello World
b) Bye
c) Hello worldBye
d) Error
Answer: a
Clarification: 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”.

To practice all technical interview questions on PHP,

Leave a Reply

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