Forum Moderators: coopster

Message Too Old, No Replies

Global Vars

         

Frank_Rizzo

12:04 am on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do I really have to issue a global statement for a var from within a function? It seems a bit of a waste to me but its the only way I can get the var to be 'global'

*******************************
<?php

$data1 = 1;

for($i=1; $i < 1000; $i++) {
cool_func();
}

exit;

function cool_func() {
global $data1;
print $data1;
}

********************************

Now that works fine! And if I remove the global $data1; from the function it won't display - just as expected.

So I got the syntax right. I'm just wondering why the global HAS to go within the function. Why can't it be at the top of the code?

I have to call the function 1000 times so it seems a waste of resources to declare the global 1000 times?

dingman

12:15 am on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$foo = "foo\n";

function printbar() {
$foo = "bar\n";
echo $foo;
}

echo $foo;
printbar();
echo $foo;

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

Output would be:
foo
bar
foo

the global variable $foo is a different variable from the $foo inside printbar(). You have to declare it as global so that PHP knows you mean to be talking about the $foo in the global namespace rather than the local namespace.

Get yourself going on a big project, and you'll really appreciate this!

dingman

12:27 am on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Example of why this is nice. Suppose that cool_func() had it's own for loop in it, and also used the variable $i as the counter. Entirely plausible - I almost always use $i as my loop counter. As things are, this is safe:


for($i=1; $i < 1000; $i++) {
cool_func();
}

function cool_func(){
for ($i=0; $i < 20; $i++) {
; // Doesn't matter for the example
}
}

If any variable whose name was the same as one defined in the global scope were simply treated as a reference to that global variable, then this would be an infinite loop. Obviously, the correct behavior is to do something 20,000 times, not infinitely many :)

toadhall

12:54 am on Jan 22, 2003 (gmt 0)

10+ Year Member



If that extra line (global declaration) bothers you it could be tightened up using the $GLOBALS[] array:

function cool_func() {
print $GLOBALS['data1'];
}
$data1 = 1;
cool_func();

T

hakre

1:09 am on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi Frank_Rizzo,

there is another way, to get the var $data1 kind of 'global':


function cool_func(&$t)
{
print $t;
$t = 'printed:'.$t;
}

and pass $data1 as parameter to your function:


cool_func($data1);

that's normally what a function is for, you won't need global vars in it. you can pass vars by reference (the &) to modify them in the function (if you need to).

Frank_Rizzo

3:33 pm on Jan 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, passing the vars sounds good.

I'm looking for the fastest way though. I'll do some benchtesting.

PS,
I thought there was an option with PHP to compile with global vars switched on but that is a security risk?

dingman

4:32 am on Jan 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Different kettle of fish entirely. What you are thinking of is the "register_globals" setting in php.ini. If register globals is turned on, then any variable passed as part of a GET or POST request is instantiated as a global variable of the same name. You still have to declare them as globals to have access to them within functions. But yes, depending on how the logic of your code works, register_globals could be a security risk.