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
In the scope in which array_push()
is called, $printed
was never initialized. Either declare it as global
or include it in the function parameters:
$printed = array();
.
.
.
function dayAdvance ($startDay, $endDay, $weekType){
global $printed;
.
.
.
}
OR
function dayAdvance ($startDay, $endDay, $weekType, $printed = array()) { ... }
NOTE:
A faster alternative to array_push()
is to simply append values to your array using []
:
$printed[] = $newdateform;
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 $printed
to persist outside of the function, you must either pass it by reference or declare it as global
. The above examples are NOT equivalent. The following example would be equivalent to using global
(and is, in fact, a better practice than using global
- it forces you to be more deliberate with your code, preventing accidental data manipulation):
function dayAdvance ($startDay, $endDay, $weekType, &$printed) { ... }