obay Posted August 2, 2007 Share Posted August 2, 2007 $myvar = "hello"; function fxn() { /*[b]how do i access $myvar from here? excluding global variables and excluding passing variables on function call.. help?? [/b]*/ } fxn(); Quote Link to comment https://forums.phpfreaks.com/topic/63005-solved-how-to-access-variables-declared-outside-a-function-from-a-function/ Share on other sites More sharing options...
seikan Posted August 2, 2007 Share Posted August 2, 2007 Maybe you can try to send the value to session and get from session ? session_start(); $_SESSION['myvar'] = "hello"; function fxn(){ echo $_SESSION['myvar']; } Quote Link to comment https://forums.phpfreaks.com/topic/63005-solved-how-to-access-variables-declared-outside-a-function-from-a-function/#findComment-313768 Share on other sites More sharing options...
obay Posted August 2, 2007 Author Share Posted August 2, 2007 great! thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/63005-solved-how-to-access-variables-declared-outside-a-function-from-a-function/#findComment-313787 Share on other sites More sharing options...
ToonMariner Posted August 2, 2007 Share Posted August 2, 2007 you should try and NOT use something that depends on state if you can. If there were no session because the user had cookies off then you would get NO joy from that. keeping your code as free from state dependance is very good thing. In this case you either pass the variable to teh function or you decalre the variable as global within the function... <?php $myvar = 'hello'; // passing the variable explicitly... function fxn ($myvar) { echo $myvar; } // defining the variables scope from within the func... function fxn () { global $myvar; echo $myvar; } ?> Pros and cons to each so you decide which fits your requirements best. Quote Link to comment https://forums.phpfreaks.com/topic/63005-solved-how-to-access-variables-declared-outside-a-function-from-a-function/#findComment-313792 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.