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 { ?>
//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) {
   {     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 />";

Open in new window

Who is Participating?
Ray PaseurConnect With a Mentor Commented:
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

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
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).

<pre><?php print_r($arrcar); ?></pre>
It's showing the array as empty, though it shouldn't be.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

kentcommunicationsAuthor Commented:
I figured it out.  arrcar = array(); was inside the do...while loop. When I declare it beforehand, it's properly displaying. Thanks!
Ray PaseurCommented:
Aha!  Good catch.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.