ExamVeda
Login
Home
71
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;
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
Understanding the PHP Code:
The code defines a function named 'fun' that takes two arguments, $a and $b.
Inside the function:
  The value of $b is changed to 3.
  The function calculates $a - $b + $b - $a, which simplifies to 0 (because the $a and -a cancel each other out, and $b and -b cancel each other out).
  The function then returns this result (which is 0).
Outside the function:
  The values of $a and $b are initially set to 10 and 4 respectively.
  The function 'fun' is called with 10 and 4 as arguments, and the returned value (0) is assigned to $c.
  Finally, the code prints the values of $a, $b, and $c.
Tracing the Output:
$a remains 10 (it is not modified within the function because of pass-by-value).
$b remains 4 (the change inside the function doesn't affect the global $b).
$c becomes 0 (the return value of the function).
Therefore, the code will print "1040". Correct Option:
The correct answer is Option A: 1040
72
What will be the output of the following PHP code ?
<?php
$a = "$winner";
$b = "/$looser";
echo $a,$b;
?>
Discuss
Answer & Solution
Answer: Option C
Solution:
Since variables $winner and $looser is not defined we only see / as output.
73
What will be the output of the following PHP code ?
<?php
$a = "$winner";
$b = "$looser";
echo $a, $b;
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
As there is a backslash before $ it takes it as a string and not a variable therefore we get $looser as the output.
74
What will be the output of the following PHP code ?
<?php
$a = "$winner";
$b = "$looser";
echo $a, $b;
?>
Discuss
Answer & Solution
Answer: Option C
Solution:
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.
75
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;
?>
Discuss
Answer & Solution
Answer: Option C
Solution:
You can access the global variable using $GLOBALS[‘globalvariablename’].
76
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;
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
The value of global variable y does not change therefore it’ll print 10;
77
What will be the output of the following PHP code ?
<?php
function fun()
{
    $x = 0;
    echo $x;
    $x++;
}
fun();
fun();
fun();
?>
Discuss
Answer & Solution
Answer: Option C
Solution:
Every time the function is called the value of x becomes 0, therefore we get 0 on every function call.
78
What will be the output of the following PHP code ?
<?php
function fun()
{
    static $x = 0;
    echo $x;
    $x++;
}
fun();
fun();
fun();
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
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.
79
What will be the output of the following PHP code ?
<?php
static $x = 0;
function fun()
{
    echo $x;
    $x++;
}
fun();
fun();
fun();
?>
Discuss
Answer & Solution
Answer: Option C
Solution:
Since variable x is not defined inside the function fun(), nothing will be printed.
80
What will be the output of the following PHP code ?
<?php
$x=0;
function fun()
{
    echo $GLOBALS['x'];
    $GLOBALS['x']++;
}
fun();
fun();
fun();
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
Since, we are using $GLOBALS[‘x’], the question is similar to question 7.