This is my code, and when I run this function I get this :Warning: array_push() expects parameter 1 to be array However I define $printed as an array prior to starting.
$printed = array();
function dayAdvance ($startDay, $endDay, $weekType){
$newdateform = array(
'title' => date("M d", strtotime($startDay))." to ".date("M d", strtotime($endDay)). $type,
'start' => $startDay."T08:00:00Z",
'end' => $startDay."T16:00:00Z",
'url' => "http://aliahealthcareer.com/calendar/".$_GET['fetching']."/".$startDate);
array_push($printed, $newdateform);
if ($weekType=="weekend"){
$days="Saturday,Sunday";
}
if ($weekType=="day"){
$days="Monday,Tuesday,Wednesday,Thuresday,Friday";
}
if ($weekType=="evening"){
$days="Monday,Tuesday,Wednesday";
}
$start = $startDate;
while($startDay <= $endDay) {
$startDay = date('Y-m-d', strtotime($startDay. ' + 1 days'));
$dayWeek = date("l", strtotime($startDay));
$pos = strpos($dayWeek, $days);
if ($pos !== false) {
$newdateform = array(
'title' => date("M d", strtotime($start))." to ".date("M d", strtotime($endDate)). $type,
'start' => $startDate."T08:00:00Z",
'end' => $startDate."T16:00:00Z",
'url' => "http://aliahealthcareer.com/calendar/".$_GET['fetching']."/".$startDate);
array_push($printed, $newdateform);
}
}
}
In the scope in which
array_push()is called,$printedwas never initialized. Either declare it asglobalor include it in the function parameters:OR
NOTE:
A faster alternative to
array_push()is to simply append values to your array using[]:This method will automatically detect if the variable was never initialized, and convert it to an array prior to appending the data (in other words, no error).
UPDATE:
If you want the value of
$printedto persist outside of the function, you must either pass it by reference or declare it asglobal. The above examples are NOT equivalent. The following example would be equivalent to usingglobal(and is, in fact, a better practice than usingglobal– it forces you to be more deliberate with your code, preventing accidental data manipulation):