Goat Posted February 19, 2010 Share Posted February 19, 2010 It is a bit hard to explain so I'll just show an example: function foo() { $a = "changed by variable function"; } function bar($var_function) { $a = "original value"; $var_function(); echo($a); } bar(foo); // outputs "original value", not "changed by variable function" I can access global scope using global keyword, but there does not seem to be a way to access local function variables. Any ideas? regards, Goat Quote Link to comment https://forums.phpfreaks.com/topic/192642-acessing-outher-function-variable-from-variable-function/ Share on other sites More sharing options...
sader Posted February 19, 2010 Share Posted February 19, 2010 if u want to retrive variables from function then function must return it function foo() { $a = "returned by foo()"; return $a; } echo foo(); Quote Link to comment https://forums.phpfreaks.com/topic/192642-acessing-outher-function-variable-from-variable-function/#findComment-1014863 Share on other sites More sharing options...
Alex Posted February 19, 2010 Share Posted February 19, 2010 You would need to pass a reference of $a. This way instead of actually passing the value of $a you're passing the location in memory of $a. function foo(&$a) { $a = "changed by variable function"; } function bar($var_function) { $a = "original value"; $var_function($a); echo($a); } echo bar('foo'); Quote Link to comment https://forums.phpfreaks.com/topic/192642-acessing-outher-function-variable-from-variable-function/#findComment-1014865 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.