PHP Variables
- What will be the output of the following PHP code ?
<?php
$n1 = 15;
$n2 = 11;
$R = "$n1 + $n2";
echo "$R";
?>
-
View Hint View Answer Discuss in Forum
15 + 11
Correct Option: A
Variable R will store 15 + 11 because 15 + 11 is given in double-quotes.
- What will be the output of the following PHP code ?
<?php
$m = 15;
$n = 8;
$p = fun(15,8);
function fun($m,$n)
{
$n = 7;
return $m - $n + $n - $m;
}
echo $m;
echo $n;
echo $p;
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
The value returned from the function is 0, and value of m is 15, value of n is 8 and p is 0.
- What will be the output of the following PHP code ?
<?php
$p=7;
function calc()
{
echo $GLOBALS['p'];
$GLOBALS['p']++;
}
calc();
calc();
calc();
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
Since, we are using $GLOBALS[‘p’].
- What will be the output of the following PHP code ?
<?php
static $n = 5;
function calc()
{
echo $n;
$n++;
}
calc();
calc();
calc();
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Since variable n is not defined inside the function fun(), nothing will be printed.
- What will be the output of the following PHP code ?
<?php
function calc()
{
static $p = 5;
echo $p;
$p++;
}
calc();
calc();
calc();
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
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.