PHP Variables
- What will be the output of the following PHP code ?
<?php
$t = 2;
function calculation()
{
echo $GLOBALS['t'];
$t++;
}
calculation();
calculation();
calculation();
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
Every time the function is called the value of t becomes 2, therefore we get 2 on every function call.
- 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.
- 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.