Azu Posted February 13, 2007 Share Posted February 13, 2007 Please tell me what is a simple way to make all variables global everywhere? So that when I declare a variable somewhere in my script it will work everywhere without me having to write "global $variablea,$variable1,$variablex,$variableblablablabla,$grrrrr,$zomg,$wtf,$asdf,$varrrriiibbbllle;" in every single function which is annoying as hell? Oh and I've tried using the $globals array but it doesn't work on my server so I can't use that. P.S. my arrays and objects also need to work everywhere Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted February 13, 2007 Share Posted February 13, 2007 Pass your variables as parameters instead. function foo($foobar) { echo '<b>' . $foobar . '</b>'; } $bar = 'hello!'; foobar($bar); If you have lots of variables to parse to the function then add them into an array instead. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 13, 2007 Share Posted February 13, 2007 If $GLOBALS['variable1'] doesn't work, there is no way other than listing out your globals or passing them into the function as wildteen suggested. Function scope is there for a design reason and a technical reason, I'd suggest using OOP if you want functions that can share variables Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 With non-arrays and objects, you can make them constants. Otherwise, you'll have to use the above suggestions Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 13, 2007 Share Posted February 13, 2007 Every variable that is defined is put into the $GLOBALS superglobal array and can be referenced that way: <?php function somefunc($var) { echo "Passed variable: $var<br>"; echo 'Global var $somevar = ' . $GLOBALS['somevar'] . '<br>'; } $somevar = 'This is a test'; somefunc('This is a passed value'); ?> Ken Quote Link to comment Share on other sites More sharing options...
Azu Posted February 13, 2007 Author Share Posted February 13, 2007 I still would have to go through every single function and change them all though. Isn't there a way to just remove the function scope or whatever? And what is "OOP"? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 13, 2007 Share Posted February 13, 2007 Isn't there a way to just remove the function scope or whatever? And what is "OOP"? OOP = object oriented programming And no, you can't just remove function scope, every running function is added to the stack and that function's frame holds it's own variables, you cant seperate the variables from the stack like you want unless you use OOP where the instance variables are stored in the heap... This is assuming I remembered everything correctly and PHP does it like java/c/other languages (correct please jesi? I probably said something wrong) Quote Link to comment Share on other sites More sharing options...
Azu Posted February 13, 2007 Author Share Posted February 13, 2007 Oh okay thanks. Can you please tell me how to enable OOP? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 13, 2007 Share Posted February 13, 2007 it's not something you enable. http://www.phpdeveloper.org/news/5719 but it's a bit much if you just want to overcome variable scope. Quote Link to comment Share on other sites More sharing options...
Azu Posted February 14, 2007 Author Share Posted February 14, 2007 Okay thanks. I guess there isn't any way to get them working without rewriting all my code. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.