For some reason this function won't return the value ciao
:
$a = "ciao";
function a() {
return $a;
}
I have no idea why.
For some reason this function won't return the value ciao
:
$a = "ciao";
function a() {
return $a;
}
I have no idea why.
Functions can only return variables they have in their local space, called scope:
$a = "ciao";
function a() {
$a = 'hello`;
return $a;
}
Will return hello
, because within a()
, $a
is a variable of it's own. If you need a variable within the function, pass it as parameter:
$a = "ciao";
function a($a) {
return $a;
}
echo a($a); # "ciao"
BTW, if you enable NOTICES to be reported (error_reporting(-1);
), PHP would have given you notice that return $a
in your original code was using a undefined variable.