|
Posted by J.O. Aho on 11/06/06 15:59
kenoli wrote:
> I'm running into problems related to scope and am wondering if anyone
> knows of a good article or resource discussing it.
>
> I've ended up just making variables global a lot, which works,
> generally, but which I am told is not good practice.
>
> The kind of things that are perplexing me are things like:
>
> 1. Passing a variable to a function that is in an include file and
> then getting a variable back that is generated by that function for use
> in the originating script.
>
> 2. Exactly what using "return" in a function does regarding scope.
> Sometimes it doesn't seem to actually make the "returned" variable
> available where I want to use it.
>
> Any ideas for dealing with scope issues would be appreciated.
There are different ways to return values, the most basic is
function fun1($varin) {
return $varin+2;
}
$varin=10;
//fun1() returns a value, you need to save it to a variable
$varout=fun1($varin);
echo $varout . "\n"; // returns 12
echo $varin . "\n"; // returns 10
or with references
function fun2(&$var) {
$var+=2;
}
$varin=10;
//fun2() modifies a variable and don't return any variables
$varout=fun2($varin);
echo $varout . "\n"; // returns NULL
echo $varin . "\n"; // returns 12
So you have to know what way a function does return values, the best palce to
see that is on the online manual at www.php.net, when it comes to your own
functions you have to design them in the way you want them to return values.
//Aho
Navigation:
[Reply to this message]
|