Re: [PHP4BETA] require behaviour
| From: | Stefan Lindmark | Date: | Fri, 17 Dec 1999 15:14:31 +0000 |
| Subject: | Re: [PHP4BETA] require behaviour | ||
| References: | 1 2 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-8058@lists.php.net to get a copy of this message | ||
As I see it, the require would be read once to expand the code like
this:
<?
$A = 1;
function test() {
$A = 2;
function testb() {
.....
}
}
test();
print($A);
?>
$A = 2 sets the function-local variable $A. The global $A remains == 1.
Nesting of functions is OK. From the manual on functions, first page:
"Any valid PHP code may appear inside a function, even other functions
and class definitions."
The workaround would be to the declare $A global in test(), i.e. first
in b.php:
function test() {
global $A;
$A = 2;
:
This would give you the result you want.
Cheers,
§
Stefano Bagnara wrote:
>
> i got 2 files: a.php and b.php
>
> a.php
> <?
> $A = 1;
> function test() {
> require (b.php);
> }
> test();
> print($A);
> ?>
>
> b.php
> <?
> $A = 2;
> function testb() {
> .....
> }
> ?>
>
> is it right that it prints 1?? and, if so, why in b.php the $A is not global
> but the function declaration still works?
> is there a workaround?
>
Attachment: [text/x-vcard] Card for Stefan Lindmark stefan.lindmark.vcf
Attachment: [text/x-vcard] Card for Stefan Lindmark stefan.lindmark.vcf