idontkno Posted February 15, 2010 Share Posted February 15, 2010 "configuration.ini" [feb] skip = 15, 16, 17 <?PHP $ini_array = parse_ini_file("configuration.ini", true); $febskip = $ini_array['feb']['skip']; function create_month_array($month, $year) { $curDay = mktime(0,0,0,$month,1,$year); $numDays = (int) date('t', $curDay); switch($month){ case 2: $skip = array($febskip); break; } $pass = array(); $days = array(); for ($i = 1, $prev = 1; $i <= $numDays; ++$i, $curDay = strtotime('+1 day', $curDay)) { if (date('N', $curDay) >= 6) { $days[$i] = null; } else if (in_array($i, $skip)) { $prev = !$prev; $days[$i] = null; } else if (in_array($i, $pass)) { $days[$i] = null; } else { $prev = $days[$i] = (int) !$prev; } } echo $febskip; var_dump($febskip); //var_dump($days); } When dumped, the string shows up as NULL, and its screwing up my switch. How would I fix this? Quote Link to comment https://forums.phpfreaks.com/topic/192091-string-shows-up-as-null-when-it-isnt/ Share on other sites More sharing options...
Catfish Posted February 15, 2010 Share Posted February 15, 2010 test that the parse_ini_file() call is succeeding: if ($ini_array = parse_ini_file("configuration.ini", true)) { // etc. } else print('Could not parse INI file: configuration.ini<br/>'."\n"); If it is not, then there's your problem. Quote Link to comment https://forums.phpfreaks.com/topic/192091-string-shows-up-as-null-when-it-isnt/#findComment-1012437 Share on other sites More sharing options...
khr2003 Posted February 15, 2010 Share Posted February 15, 2010 why not put these two line within the function rather than outside it: $ini_array = parse_ini_file("configuration.ini", true); $febskip = $ini_array['feb']['skip']; variables within function are local. so you might want to try this: <?PHP function create_month_array($month, $year) { $ini_array = parse_ini_file("configuration.ini", true); $febskip = $ini_array['feb']['skip']; $curDay = mktime(0,0,0,$month,1,$year); $numDays = (int) date('t', $curDay); switch($month){ case 2: $skip = array($febskip); break; } $pass = array(); $days = array(); for ($i = 1, $prev = 1; $i <= $numDays; ++$i, $curDay = strtotime('+1 day', $curDay)) { if (date('N', $curDay) >= 6) { $days[$i] = null; } else if (in_array($i, $skip)) { $prev = !$prev; $days[$i] = null; } else if (in_array($i, $pass)) { $days[$i] = null; } else { $prev = $days[$i] = (int) !$prev; } } echo $febskip; var_dump($febskip); //var_dump($days); } Quote Link to comment https://forums.phpfreaks.com/topic/192091-string-shows-up-as-null-when-it-isnt/#findComment-1012442 Share on other sites More sharing options...
idontkno Posted February 15, 2010 Author Share Posted February 15, 2010 test that the parse_ini_file() call is succeeding: if ($ini_array = parse_ini_file("configuration.ini", true)) { // etc. } else print('Could not parse INI file: configuration.ini<br/>'."\n"); If it is not, then there's your problem. It is working, its just that for some reason, the function isn't seeing the string. I've even declared it as global and it still doesn't work. why not put these two line within the function rather than outside it: $ini_array = parse_ini_file("configuration.ini", true); $febskip = $ini_array['feb']['skip']; variables within function are local. so you might want to try this: <?PHP function create_month_array($month, $year) { $ini_array = parse_ini_file("configuration.ini", true); $febskip = $ini_array['feb']['skip']; $curDay = mktime(0,0,0,$month,1,$year); $numDays = (int) date('t', $curDay); switch($month){ case 2: $skip = array($febskip); break; } $pass = array(); $days = array(); for ($i = 1, $prev = 1; $i <= $numDays; ++$i, $curDay = strtotime('+1 day', $curDay)) { if (date('N', $curDay) >= 6) { $days[$i] = null; } else if (in_array($i, $skip)) { $prev = !$prev; $days[$i] = null; } else if (in_array($i, $pass)) { $days[$i] = null; } else { $prev = $days[$i] = (int) !$prev; } } echo $febskip; var_dump($febskip); //var_dump($days); } Actually, that was my temporary hack-y fix. Quote Link to comment https://forums.phpfreaks.com/topic/192091-string-shows-up-as-null-when-it-isnt/#findComment-1012810 Share on other sites More sharing options...
Catfish Posted February 16, 2010 Share Posted February 16, 2010 if you wanted the two lines outside the function for whatever reason, you would probably need to create another parameter for the function ot pass the value of $febskip to the function. Quote Link to comment https://forums.phpfreaks.com/topic/192091-string-shows-up-as-null-when-it-isnt/#findComment-1012971 Share on other sites More sharing options...
idontkno Posted February 16, 2010 Author Share Posted February 16, 2010 if you wanted the two lines outside the function for whatever reason, you would probably need to create another parameter for the function ot pass the value of $febskip to the function. The entire point of doing it outside of the function was in case I ever needed it again and to prevent overhead. My other alternative was to do this: function init_skip($month) { $ini_array = parse_ini_file("configuration.ini", true); switch($month) { case 1: $skip = $ini_array['jan']['skip']; break; case 2: $skip = $ini_array['feb']['skip']; break; } return $skip; } switch($month){ case 2: $skip = explode(", ", init_skip($month)); break; Quote Link to comment https://forums.phpfreaks.com/topic/192091-string-shows-up-as-null-when-it-isnt/#findComment-1012975 Share on other sites More sharing options...
PFMaBiSmAd Posted February 16, 2010 Share Posted February 16, 2010 You are missing the point of FUNCTIONS. Functions should be general purpose. It should not matter where your $ini_array comes from (parsing a .ini file, a SQLite/flat-file database, a mysql database, a PostgreSQL database ...) If you pass the data into the function as a parameter when you call the function, you can reuse your function in many different applications with the data coming form anywhere you want it to without needing to edit and then retest the code inside the function definition. Quote Link to comment https://forums.phpfreaks.com/topic/192091-string-shows-up-as-null-when-it-isnt/#findComment-1013046 Share on other sites More sharing options...
gizmola Posted February 16, 2010 Share Posted February 16, 2010 I'm not sure where you are with your code at this point. The reason your original code did not work, is because php's variable scoping in regards to "global" or "script" scope is a bit unusual. No variables other than the "super globals" are visible inside of functions. So the only way you see a variable in a function is either: a) as a parameter b) a super global ($_ variables as defined by PHP) c) declared to be global INSIDE the function using the "global" keyword. d) Referenced from the $GLOBALS[] superglob array (ie. $GLOBALS['febskip']). Personally you should strive for the design PFMaBiSmAd described, and I'd recommend you pass in the data you need. I also don't see why you would use the .ini file format when it would be easier for you to have a configuration file in a pre-made array format. Quote Link to comment https://forums.phpfreaks.com/topic/192091-string-shows-up-as-null-when-it-isnt/#findComment-1013065 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.