scalar value as an array PHP Origin: Application & API OSI layer 7 · Application Last updated 2 Dec 2026

PHP Fatal error: Uncaught Error: Cannot use a scalar value as an array

Code assigned to $x[...] where $x holds a number, string or boolean.

Exact message you may see Cannot use a scalar value as an arrayUncaught Error Cannot use a scalar value as an array
Seen on: PHP

Meaning

PHP 7.1+ throws once a variable holds a scalar (true, 1, 'text') and you try to append or index. false/null auto-create arrays only in some cases.

Common causes

  • Variable previously assigned a scalar (e.g., $a = 1; $a[] = 2)
  • Function returning false on failure used as an array
  • Reusing a variable name
  • Config value set as string, treated as array

⚡ Quick fix

  1. Initialise $x = [] before use
  2. Check function return values
  3. Rename variables

Detailed fix by platform

PHP

  1. $items = []; $items[] = $row;

How to diagnose

  1. Type — var_dump before use?
  2. Reuse — Variable overwritten?
  3. Return — false on failure?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

Array offset on null — Warning: Trying to access array offset on value of type null (PHP)Array offset on nullArray to string conversion — Warning: Array to string conversion (PHP)Array to string conve…Cannot use object as array — Fatal error: Uncaught Error: Cannot use object of type stdClass as array (PHP)Cannot use object as …Illegal string offset — PHP Warning: Illegal string offset 'x' / TypeError: Cannot access offset of type string on string (PHP)Illegal string offsetImplicit float to int — Deprecated: Implicit conversion from float 2.5 to int loses precision (PHP)Implicit float to intTypeError — Fatal error: Uncaught TypeError: foo(): Argument #1 ($x) must be of type int, string given (PHP)TypeErrorTypeError: Argument #1 must be of type X, Y given — Laravel/PHP: TypeError: App\Services\Mailer::send(): Argument #1 ($user) must be of type App\Models\User, null given (Laravel)TypeError: Argument #…Undefined array key — Warning: Undefined array key / Undefined index (PHP)Undefined array keyscalar va…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.