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? 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. 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 } } 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. 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! 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. Link to comment https://forums.phpfreaks.com/topic/268394-help-global-variable/#findComment-1378125 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.