New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
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!).
$arrMessages = array(); function AddMessage($array, $msg) { return array_push($array, $msg); } Or you can typecast: function AddMessage(array $array, $msg) { return array_push($array, $msg); } Or as Corey mentioned: $arrMessages[] = $strMsg;Agreed. Even the PHP docs say to use this method instead of array_push.
From PHP Docs ->
changed it to $arrMessages[] = $strMsg;
thanks!