This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Variables – 4”.
1. What will be the output of the following PHP code?
<?php
$a = "$winner";
$b = "/$looser";
echo $a,$b;
?>
a) $winner/$looser
b) /$looser
c) /
d) $looser
View Answer
Explanation: Since variables $winner and $looser is not defined we only see / as output.
2. What will be the output of the following PHP code?
<?php
$a = "$winner";
$b = "\$looser";
echo $a, $b;
?>
a) $winner\$looser
b) \$looser
c) \
d) $looser
View Answer
Explanation: As there is a backslash before $ it takes it as a string and not a variable therefore we get $looser as the output.
3. What will be the output of the following PHP code?
<?php
$a = "$winner";
$b = "\\$looser";
echo $a, $b;
?>
a) $winner\\$looser
b) \\$looser
c) \
d) $looser
View Answer
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.
4. What will be the output of the following PHP code?
<?php
$x = 5;
$y = 10;
function fun()
{
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
fun();
echo $y;
?>
a) 5
b) 10
c) 15
d) Error
View Answer
Explanation: You can access the global variable using $GLOBALS[‘globalvariablename’].
5. What will be the output of the following PHP code?
<?php
$x = 5;
$y = 10;
function fun()
{
$y = $GLOBALS['x'] + $GLOBALS['y'];
}
fun();
echo $y;
?>
a) 5
b) 10
c) 15
d) Error
View Answer
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?
<?php
function fun()
{
$x = 0;
echo $x;
$x++;
}
fun();
fun();
fun();
?>
a) 012
b) 123
c) 000
d) 111
View Answer
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?
<?php
function fun()
{
static $x = 0;
echo $x;
$x++;
}
fun();
fun();
fun();
?>
a) 012
b) 123
c) 111
d) Error
View Answer
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?
<?php
static $x = 0;
function fun()
{
echo $x;
$x++;
}
fun();
fun();
fun();
?>
a) 012
b) 123
c) Nothing
d) Error
View Answer
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?
<?php
$x=0;
function fun()
{
echo $GLOBALS['x'];
$GLOBALS['x']++;
}
fun();
fun();
fun();
?>
a) 000
b) 012
c) 123
d) Error
View Answer
Explanation: Since, we are using $GLOBALS[‘x’].
10. What will be the output of the following PHP code?
<?php
$x = 0;
function fun()
{
echo $GLOBALS['x'];
$x++;
}
fun();
fun();
fun();
?>
a) 000
b) 012
c) Nothing
d) Error
View Answer
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 all areas of PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check Information Technology Books
- Check PHP Books
- Check MCA Books
- Practice MCA MCQs
- Apply for Programming Internship