PHP Variables
- What will be the output of the following PHP code ?
<?php
function calc()
{
$n = 5;
echo $n;
$n++;
}
calc();
calc();
calc();
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
Every time the function is called the value of n becomes 5, therefore we get 5 on every function call.
- What will be the output of the following PHP code ?
<?php
$p = 15;
$q = 25;
function calc()
{
$q = $GLOBALS['p'] + $GLOBALS['q'];
}
calc();
echo $q;
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
The value of global variable y does not change therefore it’ll print 25;
- What will be the output of the following PHP code ?
<?php
$n1 = 15;
$n2 = 18;
function Calc()
{
$GLOBALS['n2'] = $GLOBALS['n1'] + $GLOBALS['n2'];
}
Calc();
echo $n2;
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
You can access the global variable using $GLOBALS[‘globalvariablename’].
- What will be the output of the following PHP code ?
<?php
$str1 = "$Test";
$str2 = "\\$Example";
echo $str1, $str2;
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
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.
- What will be the output of the following PHP code ?
<?php
$p = "$Test";
$q = "\$Example";
echo $p, $q;
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
As there is a backslash before $ it takes it as a string and not a variable therefore we get $Example as the output.