[Solved] array_push() expects parameter 1 to be array, null given php error message


You get the error : Warning: array_push() expects parameter 1 to be array, null given

because your $_SESSION['messages'] variable is never set.

When this part of your code gets executed,

session_start();
if (session_status() == PHP_SESSION_NONE) {
    $_SESSION['messages'] = array();  
}

The first line starts a new session. So session_status() == PHP_SESSION_NONE will never be true since session_status() returns 2 and PHP_SESSION_NONE equals to 1. As a result, $_SESSION[‘messages’] = array(); will not get executed.

What you need to be doing is, check if a session has been started, and start one if not.

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

This will check if you have a session_start() called somewhere earlier in your script.

Then after that, add this line:

if(!isset($_SESSION['messages']))
    $_SESSION['messages'] = array();

Hope it helps.

0

solved array_push() expects parameter 1 to be array, null given php error message