php: pushing to an array that may or may not exist
I want to create an array with a message. $myArray = array('my message'); But using this code, myArray will get overwritten if it already existed. If I use array_push , it has to already exist. $myArray = array(); // <-- has to be declared first. array_push($myArray, 'my message'); Otherwise, it will bink. Is there a way to make the second example above work, without first clearing $myArray = array(); ? Check if the array exists first, and if it doesn't, create it...then add the element, knowing that the array will surely be defined before hand : if (!isset($myArray)) { $myArray = array(); }