I am struggling understanding how this code works. when we call the function with a value 5, the value of c, becomes 5. Now 5 + 25 = 30 . What i don't understand is how the value of the $_GET['c']
equals to 25, when it should be 5.
my answer would therefore be 5 + 25 - 5 - 10 = 15.
<?php
function process($c, $d = 25){
global $e;
$retval = $c + $d - $_GET['c'] - $e;
return $retval;
}
$e = 10;
echo process(5);
?>
Its variables should be getting another value, maybe you use "global" you might be picking up the value from another location.
Try show variables:
<?php
function process($c, $d = 25){
global $e;
echo '$e: ', $e , '<br>';
echo '$c: ', $c , '<br>';
echo '$d: ', $d , '<br>';
echo '$_GET['c']: ', $_GET['c'] , '<br>';
$retval = $c + $d - $_GET['c'] - $e;
echo $c ,' + ' , $d , ' - ' , $_GET['c'] , ' - ' , $e , ' = ' , $retval , '<hr>';
return $retval;
}
$e = 10;
echo process(5);
?>
So you will probably find the variable problem.
In some cases you can use a static function in a class (php object oriented)