walexman Posted October 23, 2007 Share Posted October 23, 2007 In php i've found that I cant seem to access global variables from within a function. Is there a way to access variables in a function that are in the same scope that the function is??? Can you make a variable global or something? How does that all work in PHP, is there a doc somewhere to explain? Can I define a global within a function in php? Quote Link to comment https://forums.phpfreaks.com/topic/74464-pass-variables-to-functions/ Share on other sites More sharing options...
BlueSkyIS Posted October 23, 2007 Share Posted October 23, 2007 to retrieve an external global variable, declare the variable global from within the function: $somevar = 1.234; function abcdefg() { global $somevar; // $somevar here is the same as the global one. } Quote Link to comment https://forums.phpfreaks.com/topic/74464-pass-variables-to-functions/#findComment-376239 Share on other sites More sharing options...
otuatail Posted October 23, 2007 Share Posted October 23, 2007 You can have a session variable if you want to access it anywhere, but you can pass variables to a function myfunction($cats, $dogs); Desmond. Quote Link to comment https://forums.phpfreaks.com/topic/74464-pass-variables-to-functions/#findComment-376244 Share on other sites More sharing options...
walexman Posted October 23, 2007 Author Share Posted October 23, 2007 how do I declare a session variable? Do I have to use global $variable within a function in order to access a global variable within a function as well? Quote Link to comment https://forums.phpfreaks.com/topic/74464-pass-variables-to-functions/#findComment-376254 Share on other sites More sharing options...
Orio Posted October 23, 2007 Share Posted October 23, 2007 About variables' scope and global variables, read here. About session variables, you need to call session_start() at every page you want to use the session variables, and then you store the session data in the $_SESSION array. More info here. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/74464-pass-variables-to-functions/#findComment-376256 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.