PHP 8.1 warns that false is automatically converted to an array
Deprecated: Automatic conversion of false to array is deprecatedShort answer
Since PHP 8.1, writing $x[] = 1 or $x['k'] = 1 when $x is false is deprecated. The fix is to give the variable an array before you write to it. If the line sits in a vendor library such as ADOdb, update that library to a release that fixes it.
Fix it fast
1Find out which cause you have
Read the file and line number in the message. A path under your own source tree means cause 1, a path under vendor/ or a library folder means cause 2.
php -v2Match what you see, then jump to its fix
- 1
The line is in your own code, for example $rows[] = $xYour code appends to a variable that is falseFix - 2
The variable gets false from a call that can fail, then is used as an arrayA function returned false and the code did not handle itFix - 3
The path points to vendor/ or a library folder, not your codeThe line is inside a library such as ADOdbFix
- 1
What does the error look like?
Deprecated: Automatic conversion of false to array is deprecated
Cause 1 of 3
Your code appends to a variable that is false
1Check
The PHP 8.1 migration guide lists autovivification from false as deprecated, while creating an array from null or an undefined variable is still allowed. Find where the variable gets its value. A function that returns false on failure is a common source.
<?php
$arr = false;
$arr[] = 2; // Deprecated in 8.12Fix
Initialize the variable with an array. The array chapter of the manual also says it is better to initialize a variable by a direct assignment than to rely on automatic creation.
<?php
$arr = [];
$arr[] = 2;3Verify
Run the page again. The deprecation line should be gone from the output and the log.
php -d error_reporting=-1 -d display_errors=1 script.phpConfirmed by php.net
Cause 2 of 3
A function returned false and the code did not handle it
1Check
Print the value just before the failing line to see whether it is false.
<?php
var_dump($result);2Fix
Handle the failure branch, or default to an empty array, before you write into the value.
<?php
$result = some_call();
if ($result === false) {
$result = [];
}
$result['key'] = 'value';3Verify
Run the failing path on purpose, for example with a bad input, and confirm no deprecation appears.
php script.phpConfirmed by php.net
Cause 3 of 3
The line is inside a library such as ADOdb
1Check
The ADOdb changelog lists fixes for this exact message: 5.22.2 for the mssql driver and 5.22.7 for oci8. Check which version you have.
composer show adodb/adodb-php2Fix
Update the library to its latest release. If your driver is not named in the changelog, update anyway and report the file and line to the project.
composer update adodb/adodb-php3Verify
Repeat the request that used to print the deprecation.
composer show adodb/adodb-php | head -n 5Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Initialize arrays with [] before you fill them, and do not rely on automatic creation.
- Run your tests with error_reporting set to -1 on the PHP version you deploy to, so deprecations show up before production.
- Keep vendor libraries on current releases when you move to a new PHP minor version.
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
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 fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix