Making a function work, problem with variables

I'm getting this error when I run my code:

Notice: Undefined variable: arrcar in test.php on line 166
I know that the error, involving arrcar, is only happening if the column "update" is completely empty. If there is any data, including data that doesn't meet the requirements of $ndays, it works fine. How do I set arrcar in the function so that, even if the update column is empty, the page won't display an error message?
<?php do { ?>
  <?php
				
//tests to see if the last change value is empty (therefore never set)
if (isset($row_taskscar['update'])) {
				
$thisday = date("Y-m-d");
						
						
//sets the last change date to a variable $date
$date = $row_taskscar['update']; 
 
//format the date for mktime() function -- you need day, month, year
$year = substr($date, 0,4); 
$month = substr($date, 5, 2); 
$day = substr($date, 8, 2); 
 
//sets the frequency to a variable $freq
$freq = $row_taskscar['freq'];
 
//sets the variable to hold the new date. Calculates the new date by 
//evaluating the lastchange date and adding the frequency amount measured in months
$new_datecar = date("Y-m-d", mktime(0, 0, 0, $month +$freq, $day , $year)); 
 
date_diff("$thisday", "$new_datecar");
						
if ($ndays <= 15) {
global $arrcar;
$arrcar[] = array('task' => $row_taskscar['task'], 'adate' => $new_datecar);
							 }	
						}
				   ?>
  <?php } while ($row_taskscar = mysql_fetch_assoc($taskscar)); 
 
function funcar($arrcar) {
if(!is_array($arrcar))
   {     return false;   }
	foreach($arrcar as $element)  {
	$car_taskelm = $element["task"];
	$car_dateelm = $element["adate"];
	echo "<b>$car_taskelm</b> is due on <b>$car_dateelm</b><br /><br />";
	}
	}
 
funcar($arrcar);
 
?>

Open in new window

kentcommunicationsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This is a notice, not an error:

Notice: Undefined variable: arrcar in test.php on line 166

At line 3 put this...
$arrcar = array();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Also, you might want to remove line 28 - a global declaration does not belong outside a function.  And since you are explicitly passing the $arrcar variable to the function, you do not need it to be global in scope.

Best, ~Ray
0
kentcommunicationsAuthor Commented:
I removed the global line, and added in $arrcar = array(); at line 3, but now nothing is displaying, even though I know there should be (and was).

Using
<pre><?php print_r($arrcar); ?></pre>
It's showing the array as empty, though it shouldn't be.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Ray PaseurCommented:
In that case the "shouldn't be (empty)" part is due to a data dependent problem of some sort.

In the snippet above $ndays is undefined, but the code tests it on line 27.  So you might want to look at that variable - where is it set?

Also, consider making your life simpler by following the indentation guidelines for conditional and control statements, like this in the code snippet.  Make the curly braces line up, and it will be much easier to read and understand the intent of the code!

HTH, ~Ray
if ($ndays <= 15) 
{
    $arrcar[] = array(
                  'task' => $row_taskscar['task'], 
                  'adate' => $new_datecar
                 );
}    

Open in new window

0
kentcommunicationsAuthor Commented:
I figured it out.  arrcar = array(); was inside the do...while loop. When I declare it beforehand, it's properly displaying. Thanks!
0
Ray PaseurCommented:
Aha!  Good catch.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.