This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Variables – 2”.
1. What will be the output of the following PHP code?
<?php
one = 1;
two = 2;
three = 3;
four = 4;
echo "one / two + three / four";
?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Explanation: 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?
<?php
$on$e = 1;
$tw$o = 2;
$thre$e = 3;
$fou$r = 4;
echo "$on$e / $tw$o + $thre$e / $fou$r";
?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Explanation: You can not use the $ in between the variable name.
3. What will be the output of the following PHP code?
<?php
$on_e = 1;
$tw_o = 2;
$thre_e = 3;
$fou_r = 4;
echo $on_e / $tw_o + $thre_e / $fou_r;
?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Explanation: You can use _ in a variable name.
4. What will be the output of the following PHP code?
<?php
$On_e = 1;
$tw_o = 2;
$thre_e = 3;
$fou_r = 4;
echo $on_e / $tw_o + $thre_e / $fou_r;
?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
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?
<?php
echo $red;
?>
a) 0
b) Nothing
c) True
d) Error
View Answer
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?
<?php
$four4 = 4;
$three3 = 3;
$two2 = 2;
echo $four4 + $three3 / $two2 - 1;
?>
a) 4.5
b) 7
c) 3.5
d) Error
View Answer
Explanation: You can use numbers in a variable name.
7. What will be the output of the following PHP code?
<?php
$4four = 4;
$3three = 3;
$2two = 2;
echo $4four + $3three / $2two - 1;
?>
a) 4.5
b) 7
c) 3.5
d) Error
View Answer
Explanation: A variable name can not start with a numeric value.
8. What will be the output of the following PHP code?
<?php
int $one = 1;
echo "$one";
?>
a) 0
b) 1
c) $one
d) Error
View Answer
Explanation: Unlike other programming languages there are no data types in PHP.
9. What will be the output of the following PHP code?
<?php
var $one = 1;
var $two = 2;
echo $one / $two * $one / $two * $two;
?>
a) 1
b) 0
c) 0.5
d) Error
View Answer
Explanation: You can not use var before a variable name.
10. What will be the output of the following PHP code?
<?php
$hello = "Hello World";
$bye = "Bye";
echo $hello;"$bye";
?>
a) Hello World
b) Bye
c) Hello worldBye
d) Error
View Answer
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 areas of PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Practice Programming MCQs
- Practice MCA MCQs
- Check PHP Books
- Apply for Programming Internship
- Check MCA Books