This set of PHP Problems focuses on “Variables – 3”.
1. What will be the output of the following PHP code?
<?php
$x;
echo "$x";
?>
a) 0
b) 1
c) Nothing
d) Error
View Answer
Explanation: Since the variable x is not initialised it is not storing any value, therefore nothing will be printed on the screen.
2. What will be the output of the following PHP code?
<?php
$x = 5;
{
$x = 10;
echo "$x";
}
echo "$x";
?>
a) 1010
b) 105
c) 510
d) error
View Answer
Explanation: Variable x stores the value 10 and not 5.
3. What will be the output of the following PHP code?
<?php
$x = 5;
{
echo "$x";
}
?>
a) 0
b) 5
c) Nothing
d) Error
View Answer
Explanation: The variable x stores the value 5 and therefore the value 5 is printed on the screen.
4. What will be the output of the following PHP code?
<?php
$x = 5;
function fun()
{
echo "$x";
}
fun();
?>
a) 0
b) 5
c) Nothing
d) Error
View Answer
Explanation: The variable x is not defined inside the function fun(), therefore nothing is printed on the screen.
5. What will be the output of the following PHP code?
<?php
$x = 5;
function fun()
{
$x = 10;
echo "$x";
}
fun();
echo "$x";
?>
a) 0
b) 105
c) 510
d) Error
View Answer
Explanation: First when the function is called variable x is initialised to 10 so 10 is printed later the global value 5 is printed.
6. What will be the output of the following PHP code?
<?php
$x = 4;
$y = 3;
function fun($x = 3, $y = 4)
{
$z = $x+$y/$y+$x;
echo "$z";
}
echo $x;
echo $y;
echo $z;
fun($x, $y);
?>
a) 43
b) 943
c) 349
d) 439
View Answer
Explanation: Firstly, the statements outside the function are printed, since z is not defined it’ll no value is printed for z. Next the function is called and the value of z inside the function is printed.
7. What will be the output of the following PHP code?
<?php
$x = 4;
$y = 3;
function fun($x, $y)
{
$z = $x + $y / $y + $x;
echo "$z";
}
echo $x;
echo $y;
echo $z;
fun(3, 4);
?>
a) 437
b) 439
c) 349
d) 347
View Answer
Explanation: It is same as above but the value passed into the function is 3,4 and not 4,3. Therefore the difference in answer.
8. What will be the output of the following PHP code?
<?php
function fun($x,$y)
{
$x = 4;
$y = 3;
$z = $x + $y / $y + $x;
echo "$z";
}
fun(3, 4);
?>
a) 7
b) 9
c) 0
d) Error
View Answer
Explanation: Value 3, 4 is passed to the function but that is lost because x and y are initialised to 4 and 3 inside the function. Therefore we get the given result.
9. What will be the output of the following PHP code?
<?php
$x = 3, 4, 5, 6;
echo "$x";
?>
a) 3
b) 4
c) 6
d) Error
View Answer
Explanation: In C you won’t get an error but in PHP you’ll get a syntax error.
10. What will be the output of the following PHP code?
<?php
$a = 10;
$b = 4;
$c = fun(10,4);
function fun($a,$b)
{
$b = 3;
return $a - $b + $b - $a;
}
echo $a;
echo $b;
echo $c;
?>
a) 1040
b) 1004
c) 1043
d) 4100
View Answer
Explanation: The value returned from the function is 0, and value of a is 10, value of b is 4 and c is 0.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all areas of PHP Problems, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Apply for Programming Internship
- Practice MCA MCQs
- Check Information Technology Books
- Practice Programming MCQs
- Check MCA Books