PHP throws Error when you write to a variable that holds true or a number
Fatal error: Uncaught Error: Cannot use a scalar value as an arrayShort 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.
Fix it fast
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 line2Match what you see, then jump to its fix
- 1
Uncaught 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
var_dump of the return value shows bool(true) or int(...).A function returned true or a number instead of an arrayFix - 3
Deprecated: Automatic conversion of false to array is deprecated (not an Error yet).The variable is false, which PHP 8.1 stopped converting quietlyFix
- 1
What does the error look like?
Fatal error: Uncaught Error: Cannot use a scalar value as an arrayDeprecated: 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.php2Fix
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.phpConfirmed by php.net
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.phpConfirmed by php.net
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.phpConfirmed by php.net
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
PHP says Illegal string offset or Cannot access offset of type string on string
Warning: Illegal string offset "1x"
You used an array key on a value that is a string.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix