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;
?>
<?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. 1410
C. 1400
D. 4100
Answer: Option A
Solution (By Examveda Team)
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
Join The Discussion
Comments (3)
Related Questions on Operators and Expressions in php
What will be the output of the following PHP code ?
<?php
"Hello World"
?>
A. Error
B. Hello World
C. Nothing
D. Missing semicolon error
What will be the output of the following PHP code ?
<?php
print_r "Hello world"
?>
A. Error
B. Hello World
C. Nothing
D. Missing semicolon error
A. redredred
B. redred
C. red
D. Error
A. # Hello world
B. Hello world# Hello world
C. Hello world
D. Error

1040
the Correct Answer is 1040
The right answer is 1040