Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

What will be the value of $a and $b after the function call in the following PHP code?
<?php
function doSomething( &$arg ) {
$return = $arg;
$arg += 1;
return $return;
}
$a = 3;
$b = doSomething( $a );
?>

What will be the value of $a and $b after the function call in the following PHP code?
<?php
function doSomething( &$arg ) {
$return = $arg;
$arg += 1;
return $return;
}
$a = 3;
$b = doSomething( $a );
?>
a) a is 3 and b is 4
b) a is 4 and b is 3
c) Both are 3
d) Both are 4

Answer: b
Explanation: $a is 4 and $b is 3. The former because $arg is passed by reference, the latter because the return value of the function is a copy of the initial value of the argument.

Join The Discussion