array_push on Non-Array

Fatal error: Uncaught TypeError: array_push(): Argument #1 ($array) must be of type array, null given

Quick Answer

You are calling array_push() on a variable that is null instead of an array. Initialize the variable as an empty array before pushing to it.

Why This Happens

array_push() requires its first argument to be an array. If the variable is null, uninitialized, or a different type, PHP throws a TypeError in PHP 8+ or a warning in earlier versions. This commonly happens when the array is conditionally initialized or comes from a function that can return null.

The Problem

$items = null;
array_push($items, 'new item'); // $items is null, not an array

The Fix

$items = [];
$items[] = 'new item'; // Simpler than array_push for single items

// Or initialize before push:
$items = $items ?? [];
array_push($items, 'new item');

Step-by-Step Fix

  1. 1

    Check the variable type

    Verify the variable is an array before calling array_push. Use var_dump() or is_array() to check.

  2. 2

    Initialize as empty array

    Ensure the variable is initialized as an empty array [] before any push operations.

  3. 3

    Use [] syntax

    For single-item pushes, use $array[] = $value instead of array_push(). It is simpler and slightly faster.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free