Set global variable from inside a function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Set global variable from inside a function?

Post by facets »

Hey,

As the subject suggests is it possible to set a global variable from inside a function?

ta./ Will
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

We encourage our users to try things for themselves before asking questions. It is only curteous that you try and resolve issues on your own first before asking someone else to put the time in to solve your concern.

So I ask, have you tried to set a global variable inside a function.

http://php.net/global may be of some interest
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

Apologies I should have posted some code or an example.
Here it is so far :

Ensure that it's initialised in the main code (not in a function)..

Code: Select all

global $total_return;
I return my variable from the function..

Code: Select all

echo $total_return;
return ($total_return);
Bring the global variable into my new function..

Code: Select all

function totals_return($total_return) {
	global $total_return; // View to see is set
        echo $total_return;
}
All with no luck.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You almost had it :wink:. Pretty much you want to avoid the usage of globals, as they are a code smell. Simply pass the value to then function and return it when your done processing (like you sort of did)

Code: Select all

function totals_return($total_return) {
   //do some funky stuff to total_return

   return $total_return;
}
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I think we may have crossed wires somewhere :)

I have removed globals $total_return;

My function where I process $total_return has the following line :

Code: Select all

return $total_return;
I then want to pull the variable into this function to print it to the browser.

Code: Select all

function totals_return($total_return) {
	print "<tr><td><font size=2 face=arial><b>Total Return Amount including GST:</b></font></td><td align=right><font face=arial size=3 color=red><b>$".number_format($total_return,"2")."</b></td></tr>";
}
This doesn't work. Have I missed your point?
Is there a function that can print all data in the buffer? To help debug.

PS. Thanks for your assistance!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

facets wrote:I have removed globals $total_return;

My function where I process $total_return has the following line :

Code: Select all

return $total_return;
I'm not exactly sure what your expecting the return to do. When calling return on the global scale, which I suspect you did, then it will terminate the current script being processed. What I think you want is

Code: Select all

function totals_return($total_return) {
	return number_format($total_return,"2");
}

print "<tr><td><font size=2 face=arial><b>Total Return Amount including GST:</b></font></td><td align=right><font face=arial size=3 color=red><b>$".totals_return($total_return)."</b></td></tr>";
Am I right?
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

thanks jcart. Fixed.
I rewrote a few things and worked out I hadn't initialised the variable in the correct function! DOH!

Anyway, now, can you 'return' 2 variables from a function?

EDIT : Of course.. Via an array.
Post Reply