Jump to content

String shows up as 'NULL' when it isn't


idontkno

Recommended Posts

"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?

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.