Gossamer Forum
Home : General : Perl Programming :

Confused with how to set a global in PHP...

Quote Reply
Confused with how to set a global in PHP...
Basically, I have two sub routines. One, called get_info() and the other show_stats(). Get_info() is called first, and then does some queries etc to get values from a SQL database. The problem I am having though is getting back the variables that were created into the show_stats() variable. I've tried array_pop() to add it to the $GLOBALS array, but that doesn't work.

Anyone got any ideas?

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
Can't you pass the variables to your second routine?
Quote Reply
Re: [RedRum] Confused with how to set a global in PHP... In reply to
Yeah, but I don't really want to do that. To make the code cleaner and a bit more easy to understand I prefer not to pass it through the subs. For that reason I am defining what subs to call at the top of the pag, like;

get_info();
show_stats();

So it executes the first sub, gets all the info, then should return the info for the show_stats one to use. But I'm not sure how to return them Frown

Thanks anyway.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
How about:

get_info(&show_stats);
Quote Reply
Re: [RedRum] Confused with how to set a global in PHP... In reply to
Nope, and FWIW you don't need the '&' in PHP Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
>>Nope<<

Nope what?

>> and FWIW you don't need the '&' in PHP <<

Thats why php is so confusing Angelic....having said that you don't need it in perl either (well depends what you are doing).

Last edited by:

RedRum: Feb 16, 2002, 7:21 AM
Quote Reply
Re: [RedRum] Confused with how to set a global in PHP... In reply to
Perl and PHP are both great languages. PHP makes SQL sooo much easier (IMO) cos it has loads of functions and stuff for it (i.e. less coding...lol)...but perl has better error catching for mail sending etc (PHP just returns true or false for the delivery, and as far as i know there is no way to catch the errors) Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
array_pop() is removing elements not adding, you need array_push()

Im basing my answer on what I know from perl, so it if it's wrong don't blame me :)
Quote Reply
Re: [RedRum] Confused with how to set a global in PHP... In reply to
Can't remember what it was I used actually :P Just array_pop rung a bell...lol.

I think I'm just gonna have to resort to putting the variables in the sub call Frown

Quote:
so it if it's wrong don't blame me :)
That was one of your old sigs here wasnt it? Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
Don't blame me if I'm wrong

Just to cover any law suits Laugh
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
In Reply To:
Nope, and FWIW you don't need the '&' in PHP
Depends on what you want to do.

Adrian
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
In Reply To:
PHP just returns true or false for the delivery, and as far as i know there is no way to catch the errors
Wrong again, it returns true or false for the delivery, BUT if you have track_errors turned on, the error message will be stored in the global variable $php_errormsg.

Adrian
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
And to answer your original question. Just go: global $VAR; in the sub to use a global variable (or to define it). Or you can define it in the global namespace (ie. not in a sub).

Adrian
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
In Reply To:
Nope, and FWIW you don't need the '&' in PHP Wink


You do if you want to pass by reference instead of creating a copy of the variable...

Code:
my_func($foo);

function my_func (&$foo) {
//stuff
}

That aside, as was stated, yes, you need to put global $your_var in the sub you want to use the global in.

You should use as few globals as possible though.
Quote Reply
Re: [Mark Badolato] Confused with how to set a global in PHP... In reply to
Is there a reason for trying to avoid a lot of globals?
Cheers,
Michael Bray
Quote Reply
Re: [brewt] Confused with how to set a global in PHP... In reply to
Wow, thanks for that. Didn't realise about $php_errormsg. I'll have to look into this more. As for the globals, I've ended up using;

Code:
$jan = getmonthstats("January");
$feb = getmonthstats("February");
$mar = getmonthstats("March");
$apr = getmonthstats("Aprile");
..etc

function getmonthstats($month) {

// do stuff here to get the stats, then return it.

return $month;

}

Its actually better as I can re-use one function for 12 methods (and it makes the code a bit cleaner, unusual for me Tongue).

Thanks.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
Calling a subroutine isn't a method is it?

Why don't you do all months at once?

my (@results) = getmonthstats(@months);

Last edited by:

RedRum: Feb 17, 2002, 2:39 AM
Quote Reply
Re: [RedRum] Confused with how to set a global in PHP... In reply to
Quote:
Calling a subroutine isn't a method is it?
Well, it works, so yeah Wink Can't you do that in Perl?

Quote:
Why don't you do all months at once?

my (@results) = getmonthstats(@months);
Cos I don't wanna Tongue Also, PHP doesn't use the @ sign to define arrays. You have to get used to these subtle diffeences between PHP and Perl.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Michael_Bray] Confused with how to set a global in PHP... In reply to
In Reply To:
Is there a reason for trying to avoid a lot of globals?


Yes. it's poor programming practice. Well, if you're doing something small it's ok but one you start getting at even a bit of complexity you should avoid them.

scope your variables to be used only where they are needed. That way it's easier to track down a problem. A global can be changed anywhere, and you could lose track of it, thus it gets changed somewhere, and you can't figure out why you're getting something different than what you're expecting.

And if you find yourself doing something like

my_sub($var1, $var2, $var3...$var18) {

(which addmittedly can grow tiresome), then find a better way to house your data instead of in seperate variables. There's nothing wrong with storing everything in an array (php) or hash (perl) and just passing that.

--mark
Quote Reply
Re: [AndyNewby] Confused with how to set a global in PHP... In reply to
I was referring to the word "method" rather than the actual code.

A method is not what you were just describing as a method, at least not in perl.

Last edited by:

RedRum: Feb 17, 2002, 8:22 AM