Examveda

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;
?>

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

This Question Belongs to PHP >> Operators And Expressions In Php

Join The Discussion

Comments (3)

  1. Mohamed Waly
    Mohamed Waly:
    10 months ago

    1040

  2. عبووود الأسطا
    عبووود الأسطا:
    4 years ago

    the Correct Answer is 1040

  3. Walid Ezzeddine
    Walid Ezzeddine:
    4 years ago

    The right answer is 1040

Related Questions on Operators and Expressions in php