Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP throws Error when you write to a variable that holds true or a number

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

Short answer

PHP throws this Error when code writes an array element, like $x['a'] = 1, into a variable that holds true, an integer or a float. Dump the variable just before the failing line to see what it really holds. Usually it was set to a scalar earlier, or a function returned true where you expected an array.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the variable's type and value on the line before the failing write. The Error tells you the container is a true, integer or float scalar.

    var_dump($settings);
    $settings['theme'] = 'dark'; // the failing line
  2. 2Match what you see, then jump to its fix

    1. 1Uncaught Error: Cannot use a scalar value as an array, and var_dump shows int, float or bool(true).The variable was assigned a number or true earlierFix
    2. 2var_dump of the return value shows bool(true) or int(...).A function returned true or a number instead of an arrayFix
    3. 3Deprecated: Automatic conversion of false to array is deprecated (not an Error yet).The variable is false, which PHP 8.1 stopped converting quietlyFix

What does the error look like?

  • Fatal error: Uncaught Error: Cannot use a scalar value as an array
  • Deprecated: Automatic conversion of false to array is deprecated

Cause 1 of 3

The variable was assigned a number or true earlier

1Check

Search the file for every assignment to that variable. A loop counter, a flag or a reused name often overwrites an array.

grep -n '\$settings' file.php

2Fix

Use different names for different things, and create arrays with an explicit assignment.

$settings = [];
$settings['theme'] = 'dark';

3Verify

Run the script again; the Error is gone and var_dump shows an array.

php file.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A function returned true or a number instead of an array

1Check

Look at what the function returns on success and on failure. The manual notes that casting a scalar with (array) gives a one-element array holding that scalar, which is rarely what you want, so check the type first.

var_dump(load_settings());

2Fix

Check the return type before you write into the value, and handle the non-array result in its own branch.

$settings = load_settings();
if (!is_array($settings)) {
    $settings = [];
}
$settings['theme'] = 'dark';

3Verify

Call the code path again with both a good and a failing input.

php file.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The variable is false, which PHP 8.1 stopped converting quietly

1Check

The manual says that as of PHP 8.1.0, creating a new array from false is deprecated, while null and undefined values are still allowed. Functions that return false on failure are the usual source.

var_dump($settings); // bool(false)

2Fix

Start from an empty array when the value is false.

$settings = $settings ?: [];
$settings['theme'] = 'dark';

3Verify

Run the script again and confirm the Deprecated line is gone from the output and the log.

php file.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Initialise every array with [] before the first write, instead of relying on auto-creation.
  • Add return types such as : array to functions, so a bool cannot slip through.
  • Run a static analyser such as PHPStan in CI; it reports writes to non-array types.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.