Page 1 of 1
Set global variable from inside a function?
Posted: Fri Mar 23, 2007 11:29 pm
by facets
Hey,
As the subject suggests is it possible to set a global variable from inside a function?
ta./ Will
Posted: Fri Mar 23, 2007 11:46 pm
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
Posted: Sat Mar 24, 2007 12:28 am
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)..
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.
Posted: Sat Mar 24, 2007 12:47 am
by John Cartwright
You almost had it

. 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;
}
Posted: Sat Mar 24, 2007 1:02 am
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 :
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!
Posted: Sat Mar 24, 2007 1:20 am
by John Cartwright
facets wrote:I have removed globals $total_return;
My function where I process $total_return has the following line :
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?
Posted: Sat Mar 24, 2007 1:44 am
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.