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: Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Solution Q695 Posted May 1, 2013 Author Solution Share Posted May 1, 2013 Should be a global, not a return Quote Link to comment 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. Quote Link to comment 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? 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.