|
Posted by J.O. Aho on 03/27/07 11:34
Christoph Burschka wrote:
> J.O. Aho schrieb:
>> The87Boy wrote:
>>
>>> I have defined some strings and functions in my script, but is there
>>> anyway I can access the strings in the function without defining them
>>> as:
>>> $n = 'Hey';
>>> function g($n='') {
>>> echo $n;
>>> }
>>> g($n)
>>>
>>
>>
>> This what you was thinking about?
>>
>> --- example 1 ---
>> $n='Hello1';
>>
>> function g() {
>> $global $n;
>> echo $n;
>> }
>>
>> g();
>> --- eof ---
>>
>> --- example 2 ---
>> $n='Hello2';
>>
>> function g() {
>> echo $GLOBALS['n'];
>> }
>>
>> g();
>> --- eof ---
> Regardless of how good the idea, both examples won't work as they shown
> are.
>
> firstly, it's "global", not "$global"
That was a type by me.
> secondly, the variable needs to
> be declared global both inside and outside of the function if they are
> to share the same scope.
It seems that the PHP documentaion disagrees with you
http://www.php.net/global
Both examples works (if removing the $ infront of global in example 1)
--
//Aho
Navigation:
[Reply to this message]
|