New on LowEndTalk? Please Register and read our Community Rules.
PHP - array_push() expects parameter 1 to be array, null given
`$arrMessages = array();
function AddMessage($strMsg)
{
global $arrMessages;
array_push($arrMessages, $strMsg);
}`
When calling AddMessage further down in the code I get array_push() expects parameter 1 to be array, null given
I'm absolutely dumbfounded by this....please help !
EDIT: Weirdest thing I've ever seen, I need to define it as a global before defining it as an array, and then define the global again inside the function....
Comments
Why not just use $arrMessages[] = $strMsg;
Think of a function as a sandbox. You need to define the input variables you're going to be using, or as you mentioned use global (which is not advised!).
Agreed. Even the PHP docs say to use this method instead of array_push.
From PHP Docs ->
changed it to $arrMessages[] = $strMsg;
thanks!