|
Posted by Steve on 11/07/06 15:28
| 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.
actually, tony, j.aho's reply was the most accurately concise description of
scope that i've seen. also, "return" is ENTIRELY in the realm of discussion
of scope! what's the difference between returning a copy of a value versus a
pointer to where a value resides? SCOPE.
don't get caught up on the op's use of the word "global". it has little to
do with his actual problem. either way, i hardly see a case for saying using
globals are a bad idea. what's the difference in a public, static variable
of a class and a global variable? SYNTAX. (and the version of php you're
using ;^)
had you read the op's question thouroughly, you wouldn't "expect that you
[he] simply haven't [hasn't] declaired [sic] variables as being global when
you [he] should have". this only shows you didn't look at his code at
all...or didn't understand it at all. his function example takes a parameter
in lieu of using global...which is good. in his example, the parameter is
$_POST. you also failed to notice that he is not changing a local copy of a
global variable...the op is RETURNING a value from within the $_POST array
via his function.
either take time to read the op's problem, question, and code, or don't
bother posting a reply.
Navigation:
[Reply to this message]
|