|
Posted by sTony on 11/07/06 11:48
"kenoli" <kenoli@igc.org> wrote in message
news:1162827495.963533.82010@b28g2000cwb.googlegroups.com...
> 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.
>
> --Kenoli
>
As I understand scope in php, variables are local unless otherwise stated
with a "global $var;". This is different from other languages I've
encountered, but it is no more or less confusing. As for your question about
getting a value from a function that's in an included file, I'd like to say
that it doesn't matter where the function is, so long as it is available to
your script, and "return" has nothing at all to do with scope. If you are
getting unecpected results, the problem is in the code. I expect that you
simply haven't declaired variables as being global when you should have, or
that you are changing a local copy of a global variable, thinking your
changing the global variable. Something simply infuriating like that. It
really is best to avoid globals though, they cause nothing but trouble.
sTony
[Back to original message]
|