Re: Scope of variables in php
| From: | Rasmus Lerdorf | Date: | Thu, 03 Aug 2000 18:19:16 +0000 |
| Subject: | Re: Scope of variables in php | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9996@lists.php.net to get a copy of this message | ||
> if (some condition) {
> // myVar pops into existence here...
> $myVar = 42;
> ...
> echo $myVar;
> }
> echo $myVar;
>
> I get the first echo right, and the second echoes nothing.
> If I add
>
> global $myVar;
> [Some of the above]
>
> both echo instructions work. Is this correct (that is, php
> vars have block-scope) ? [And if this is correct how could I
> get wrong something so obvious for such a long time ? Am I
> getting back to clueless newbie status ? :)]
There is no block scope in PHP. Your if condition must be inside a
function.
-Rasmus