Log in

View Full Version : Resolved return



ggalan
05-03-2011, 08:19 PM
why is it that when i do this, echo does not output the string?


$s = 'string';
return $s;
echo $s;

ggalan
05-03-2011, 08:28 PM
nm
"If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call. "

djr33
05-03-2011, 09:23 PM
return does two things: 1. it determines the output value for the function; 2. it stops execution of the function at the current point. If you have any complicated structures such as if statements, then it can be difficult to know exactly when the function will end. This can also be useful however:

function myfunction($x) {
if ($x==true) { return true; }
return false;
}
That is an example of using return as a default value. It acts just like an "else" statement in this case, because if the if statement was true then it would have already exited the function.


Note that in your example above it is not within a function, so I don't know what return will do in that situation.