jaymc Posted June 2, 2008 Share Posted June 2, 2008 I need to use a variable that I have set within a function, outside of that function e.g <? function beans () { $cheese = "nice"; echo "hello"; } echo $cheese; ?> Is this possible, something like setting the function globally so it can be used outside of the function container. It must use the above syntax, please dont suggest echoing $cheese within the function Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/ Share on other sites More sharing options...
whizard Posted June 2, 2008 Share Posted June 2, 2008 http://us2.php.net/global HTH Dan Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555842 Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 Impossible. All variables within a function are local to that function only. Here's a couple ways to achieve what you want though Pass by reference <?php function foo ( & $bar ) { $bar = 'Hello world'; } $var = 'sample'; echo $var; foo($var); echo $var; ?> Using the GLOBAL super variable. <?php function foo ($bar) { $GLOBALS[$bar] = 'Hello World'; } $var = 'sample'; echo $var; foo('var'); echo $var; ?> Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555847 Share on other sites More sharing options...
jaymc Posted June 2, 2008 Author Share Posted June 2, 2008 That allows me to use the variable SET outside of a function, inside a function I need to set the variable inside the function, or atleast change it, and use it outside the function Like my example above Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555848 Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 Look at my pass by reference example as well as my super variable example. Both change variables in the global scope within the function. Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555851 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 <?php function blah() { global $somevar; $somevar = "sometext"; } blah(); echo $somevar; ?> Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555854 Share on other sites More sharing options...
jaymc Posted June 2, 2008 Author Share Posted June 2, 2008 Look at my pass by reference example as well as my super variable example. Both change variables in the global scope within the function. Ok the GLOBALS is working, but 2 questions 1: Isnt this some kind of security hazard? I have always learned to turn off in php.ini 2: How can I assign multiple variables inside a function.. Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555855 Share on other sites More sharing options...
jaymc Posted June 2, 2008 Author Share Posted June 2, 2008 <?php function blah() { global $somevar; $somevar = "sometext"; } blah(); echo $somevar; ?> Cant have that as I am echoing info inside the function, so it will return everything Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555858 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 1: everything is a potential security hazard 2: same way you would make multiple vars anywhere else. declare/assign them individually or as an array, if it suits you. <?php function blah() { global $somevar; $somevar = "sometext"; } blah(); echo $somevar; ?> Cant have that as I am echoing info inside the function, so it will return everything I don't see how that matters...my script declares it inside your function just like you asked. It's declared as a global variable so you can access it outside the function just like you asked. What does it matter what you do inside your function? Do whatever you want to do. Return whatever you want to return. Notice inside my script you aren't returning anything, and yet you can still echo $somevar outside the function. Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555860 Share on other sites More sharing options...
jaymc Posted June 2, 2008 Author Share Posted June 2, 2008 1: everything is a potential security hazard 2: same way you would make multiple vars anywhere else. declare/assign them individually or as an array, if it suits you. <?php function blah() { global $somevar; $somevar = "sometext"; } blah(); echo $somevar; ?> Cant have that as I am echoing info inside the function, so it will return everything I don't see how that matters...my script declares it inside your function just like you asked. It's declared as a global variable so you can access it outside the function just like you asked. What does it matter what you do inside your function? Do whatever you want to do. Return whatever you want to return. Notice inside my script you aren't returning anything, and yet you can still echo $somevar outside the function. The problem was I had to execute that function before I call the variable, therefor it has not yet been parsed, thats why that was not working I cant move the function to the top of my script as it just echoes the contents of the function where ever is is placed, it needs to be in the center of the page wrapped in a div.. I have 90 odd pages too, $page, so cant really rejig them Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555864 Share on other sites More sharing options...
jaymc Posted June 2, 2008 Author Share Posted June 2, 2008 1: everything is a potential security hazard 2: same way you would make multiple vars anywhere else. declare/assign them individually or as an array, if it suits you. <?php function blah() { global $somevar; $somevar = "sometext"; } blah(); echo $somevar; ?> Cant have that as I am echoing info inside the function, so it will return everything I don't see how that matters...my script declares it inside your function just like you asked. It's declared as a global variable so you can access it outside the function just like you asked. What does it matter what you do inside your function? Do whatever you want to do. Return whatever you want to return. Notice inside my script you aren't returning anything, and yet you can still echo $somevar outside the function. Same as above, I need to call that function before I set <title>$pageTitle</title> If i do that, I get my page content in the HTML HEAD Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555873 Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 Register globals is different than using the GLOBALS super variable. You should avoid using globals altogether... as you can easily lose track of whats declared/defined where. Your best bet is to use a class in this case <?php class test { # Declare some variables that are public public $foo, $bar; # This will be executed as soon as you create the class # For older PHP versions use 'public function test' public function __construct ( $someValue = FALSE ) { $this->foo = 'some value'; $this->bar = $someValue; } # Internal function public function changeFoo ( $someValue ) { echo '<br>Foo used to be ' . $this->foo; $this->foo = $someValue; echo '<br>Foo is now ' . $this->foo; } } # Now we create the class $var = new test( 'another value' ); echo $var->foo . '<br>'; echo $var->bar . '<br>'; $var->foo = 'something else'; echo $var->foo . '<br>'; # Or we can use internal functions $var->changeFoo( 'classes are very handy!' ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555875 Share on other sites More sharing options...
BlueSkyIS Posted June 2, 2008 Share Posted June 2, 2008 Impossible. All variables within a function are local to that function only. ??? Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555880 Share on other sites More sharing options...
jaymc Posted June 2, 2008 Author Share Posted June 2, 2008 Register globals is different than using the GLOBALS super variable. You should avoid using globals altogether... as you can easily lose track of whats declared/defined where. Your best bet is to use a class in this case <?php class test { # Declare some variables that are public public $foo, $bar; # This will be executed as soon as you create the class # For older PHP versions use 'public function test' public function __construct ( $someValue = FALSE ) { $this->foo = 'some value'; $this->bar = $someValue; } # Internal function public function changeFoo ( $someValue ) { echo '<br>Foo used to be ' . $this->foo; $this->foo = $someValue; echo '<br>Foo is now ' . $this->foo; } } # Now we create the class $var = new test( 'another value' ); echo $var->foo . '<br>'; echo $var->bar . '<br>'; $var->foo = 'something else'; echo $var->foo . '<br>'; # Or we can use internal functions $var->changeFoo( 'classes are very handy!' ); ?> The solutions do work, but I have hit a new problem the 90+ pages i have ($page.php) either echo the contents within the function or have RAW HTML, therefor as soon as the function is executed, it spits out the contents of $page wherever it stands To get this working, I need to execute the function at the very top of my html, so I can use it inside the <TITLE> tag In doing this, I get my content before all my html, head,title,body tags etc Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555887 Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 Well, you're gonna wanna re-format those 90+ files to work the way you want. Its a crappy process, but otherwise you're just hacking a solution together. Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555892 Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 If you decide to go the 'hacky' route, simply use object buffering. http://ca.php.net/manual/en/ref.outcontrol.php Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555912 Share on other sites More sharing options...
kbh43dz_u Posted June 2, 2008 Share Posted June 2, 2008 I need to use a variable that I have set within a function, outside of that function e.g <? function beans () { $cheese = "nice"; echo "hello"; } echo $cheese; ?> Is this possible, something like setting the function globally so it can be used outside of the function container. It must use the above syntax, please dont suggest echoing $cheese within the function change it to: function beans () { $cheese = "nice"; echo "hello"; return $cheese; } $value = beans(); echo $value // == "nice" if you have more variables put them in an array and return the array. Best regards! Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555930 Share on other sites More sharing options...
jaymc Posted June 2, 2008 Author Share Posted June 2, 2008 I managed to find a way to store the contents in a variable, even when echoed from within that script function test() { ob_start(); include("page.php"); $ret = ob_get_contents(); return $ret; } page.php <table><tr><td>content here</td></tr></table> <? $pageTitle = "hello"; echo hello again; ?> <table><tr><td>test</td></tr></table> The problem now is, because of object buffering, variables I set inside page.php cant be used outside of the function/object buffer Thus rendering it a failure Quote Link to comment https://forums.phpfreaks.com/topic/108427-call-function-variable-outside-function/#findComment-555933 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.