You are here: Re: Access the string everywhere in the script « All PHP « IT news, forums, messages
Re: Access the string everywhere in the script

Posted by Christoph Burschka on 03/27/07 11:11

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 ---
>
> Both are IMHO not a good way to do, better to send a variable into the
> function (you can use references too)
>
> --- example 3 ---
> $n='Hello';
>
> function g(&$nn) {
> $nn.='3';
> }
>
> g($n);
> echo $n;
>
> --- eof ---
>

Regardless of how good the idea, both examples won't work as they shown are.

firstly, it's "global", not "$global", secondly, the variable needs to
be declared global both inside and outside of the function if they are
to share the same scope.

Examples:

global $n;
$n="hello";
g();

function g()
{
global $n;
echo $n;
}

--------------

Or

$GLOBALS['n']="hello";
g();

function g()
{
echo $GLOBALS['n'];
}

--------------

As far as I know, these are interchangeable - if you set something in
$GLOBALS['var'] array, it can be accessed by typing "global $var", and
vice versa.

--
cb

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация