Q695 Posted May 1, 2013 Share Posted May 1, 2013 inside function: echo "<br>random path: $path_rand"; return $path_rand; outside function: echo "<p>random path2: $path_rand"; $path_rand is echo in the function, but for some reason not being posted outside of the function. Why? I tried the following simple script: function test(){ $i=2; return $i; } test(); echo "X:$i"; got back: Notice: Undefined variable: in ______ on line X(echo line) X: Link to comment https://forums.phpfreaks.com/topic/277519-should-be-simple-function-return-error-what-am-i-missing/ Share on other sites More sharing options...
Barand Posted May 1, 2013 Share Posted May 1, 2013 read up on variable scope http://php.net/manual/en/language.variables.scope.php Link to comment https://forums.phpfreaks.com/topic/277519-should-be-simple-function-return-error-what-am-i-missing/#findComment-1427646 Share on other sites More sharing options...
Q695 Posted May 1, 2013 Author Share Posted May 1, 2013 I checked the function return when it was erroring the first time, but i'll try again. Link to comment https://forums.phpfreaks.com/topic/277519-should-be-simple-function-return-error-what-am-i-missing/#findComment-1427651 Share on other sites More sharing options...
Barand Posted May 1, 2013 Share Posted May 1, 2013 But you aren't using the value returned by the function - you are using a variable that was defined inside the function. As I said - variable scope Link to comment https://forums.phpfreaks.com/topic/277519-should-be-simple-function-return-error-what-am-i-missing/#findComment-1427653 Share on other sites More sharing options...
Q695 Posted May 1, 2013 Author Share Posted May 1, 2013 Should be a global, not a return Link to comment https://forums.phpfreaks.com/topic/277519-should-be-simple-function-return-error-what-am-i-missing/#findComment-1427654 Share on other sites More sharing options...
Barand Posted May 1, 2013 Share Posted May 1, 2013 Should be a global, not a return That reply deserves top spot in this site's Hall of Shame. You should NOT use globals. You should pass values to functions then use the value returned by the function. Link to comment https://forums.phpfreaks.com/topic/277519-should-be-simple-function-return-error-what-am-i-missing/#findComment-1427662 Share on other sites More sharing options...
Q695 Posted May 2, 2013 Author Share Posted May 2, 2013 Where can I find the hall of shame for responses? Link to comment https://forums.phpfreaks.com/topic/277519-should-be-simple-function-return-error-what-am-i-missing/#findComment-1427839 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.