|
Posted by Oli Filth on 11/18/05 19:08
daemon said the following on 18/11/2005 16:43:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Maarten wrote:
>
>>Here is a nice problem. I have two scripts, one calling the other with via
>>an include. The script that is included contains a variable, a function and
>>a call to that function. The variable needs to be accessible to the
>>function, so I declare it having a global scope. Then I execute the
>>function, which should echo the value of the variable. Contrary to what I
>>expected, the variable isn't there.
>>
>>The code of the 2 programs is:
>>
>>script1.php
>><?php
>>function blah()
>>{
>> include("script2.php");
>>}
>>
>>blah();
>>?>
>>
>>script2.php
>><?php
>>$foo = "bar";
>>
>>function blahblah()
>>{
>> global $foo;
>> echo $foo;
>>}
>>
>>blahblah();
>>?>
>>
>>I can't see anything wrong with the way I set this up, even more since a
>>call to script2.php does exactly what it's suppossed to do: echo the value.
>>What's going on here is beyond me. Anybody has a clue?
>>
>>Kind regards, Maarten
>>
>>
>
>
> I can... you can't expect to include another function within blah();
Yes you can.
The problem is that by declaring $foo = "bar" in what intuitively looks
like the global scope within script2.php, it's actually being declared
in the scope of blah(), and therefore isn't a global variable, and
therefore can't be accessed within blahblah().
Change script2.php to:
<?php
global $foo;
$foo = "bar";
function blahblah()
{
global $foo;
echo $foo;
}
blahblah();
?>
--
Oli
Navigation:
[Reply to this message]
|