bugzy Posted September 14, 2012 Share Posted September 14, 2012 Ok i have this variable on my edit_information.php $day = 12 on that edit_information.php I'm using a function that is located at function.php, now I want to use that $day variable on the function that I'm using.. I have tried this on edit_information.php . global $get_day; but it aint working... Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/268394-help-global-variable/ Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2012 Share Posted September 14, 2012 Post the code. Quote Link to comment https://forums.phpfreaks.com/topic/268394-help-global-variable/#findComment-1378036 Share on other sites More sharing options...
bugzy Posted September 14, 2012 Author Share Posted September 14, 2012 Post the code. edit_information.php require_once("../includes/functions.php"); $get_day = substr($get_birthdate,8,; $get_month = substr($get_birthdate,5,2); $get_year = substr($get_birthdate,0,4); global $get_day; global $get_month; global $get_year; I want to use that $get_day, $get_month and $get_year to one of the function in function.php like this function me_date_dropdown($year_limit = 0){ if($get_day == $day) { //output something here } } Quote Link to comment https://forums.phpfreaks.com/topic/268394-help-global-variable/#findComment-1378037 Share on other sites More sharing options...
trq Posted September 15, 2012 Share Posted September 15, 2012 The global keyword means nothing outside of a function. It is used: $a = 'foo'; function something() { global $a; echo $a; } something(); Having said that. Globals completely break the encapsulation that function provide. You should pass data to functions via there arguments. Quote Link to comment https://forums.phpfreaks.com/topic/268394-help-global-variable/#findComment-1378039 Share on other sites More sharing options...
bugzy Posted September 15, 2012 Author Share Posted September 15, 2012 The global keyword means nothing outside of a function. It is used: $a = 'foo'; function something() { global $a; echo $a; } something(); Having said that. Globals completely break the encapsulation that function provide. You should pass data to functions via there arguments. Now I get it! It's now working! Thanks Thorpe! Quote Link to comment https://forums.phpfreaks.com/topic/268394-help-global-variable/#findComment-1378041 Share on other sites More sharing options...
Christian F. Posted September 15, 2012 Share Posted September 15, 2012 Just to reiterate the recommended method: $a = 'foo'; function something($param) { echo $param; } something($a); Avoid using global whenever possible, which it should be just about every single time. Added: Yay! Post no 1k. Quote Link to comment https://forums.phpfreaks.com/topic/268394-help-global-variable/#findComment-1378125 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.