Access the string everywhere in the script
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)
Re: Access the string everywhere in the script
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 ---
--
//Aho
Re: Access the string everywhere in the script
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
Re: Access the string everywhere in the script
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
Re: Access the string everywhere in the script
Christoph Burschka wrote:
>
> global $n;
> $n="hello";
> g();
>
> function g()
> {
> global $n;
> echo $n;
> }
>
otherwise you might use constants (of course, if that variable value
must not be changed) which are already available in function scopes...
--
Lorenzo Bettini, PhD in Computer Science, DSI, Univ. di Firenze
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net
Re: Access the string everywhere in the script
J.O. Aho schrieb:
>
>>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)
>
>
Wow, I didn't know that. So all those commands declaring global
variables outside functions were redundant... well, you never quit learning.
--cb
Re: Access the string everywhere in the script
"Christoph Burschka" <christoph.burschka [at] rwth-aachen.de> wrote in message
news:56sheiF29re4hU1 [at] mid.dfncis.de...
|
|
| J.O. Aho schrieb:
| >
| >>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)
| >
| >
|
| Wow, I didn't know that. So all those commands declaring global
| variables outside functions were redundant... well, you never quit
learning.
while i too was about to say 'you're wrong', i do still tend to put 'global'
with variables i know i'll be using else where, if nothing else but to give
some kind of hint to the other developers that it is called within other
scopes - i.e. they may see it in a function though not passed in.
i completely avoid globals as they are very uninformative to other
developers (or myself after not working with a piece of code for a while).
and, while globals are effectively the same as a static interface on a class
object, at least that interface has a definitive structure with related
logic that will keep me from changing a property to something that is
completely invalid outside of local scope.
but that's just my 0.02 usd.