Set global variable from inside a function?
Moderator: General Moderators
Set global variable from inside a function?
Hey,
As the subject suggests is it possible to set a global variable from inside a function?
ta./ Will
As the subject suggests is it possible to set a global variable from inside a function?
ta./ Will
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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
So I ask, have you tried to set a global variable inside a function.
http://php.net/global may be of some interest
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..
Bring the global variable into my new function..
All with no luck.
Here it is so far :
Ensure that it's initialised in the main code (not in a function)..
Code: Select all
global $total_return;Code: Select all
echo $total_return;
return ($total_return);Code: Select all
function totals_return($total_return) {
global $total_return; // View to see is set
echo $total_return;
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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;
}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.
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!
I have removed globals $total_return;
My function where I process $total_return has the following line :
Code: Select all
return $total_return;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>";
}Is there a function that can print all data in the buffer? To help debug.
PS. Thanks for your assistance!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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 isfacets wrote:I have removed globals $total_return;
My function where I process $total_return has the following line :Code: Select all
return $total_return;
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>";