Maaz Zindani

Loading the index…

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

PHP 8.1 warns that false is automatically converted to an array

ErrorDeprecated: Automatic conversion of false to array is deprecated

Short 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.

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

Fix it fast

  1. 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 -v
  2. 2Match what you see, then jump to its fix

    1. 1The line is in your own code, for example $rows[] = $xYour code appends to a variable that is falseFix
    2. 2The 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. 3The path points to vendor/ or a library folder, not your codeThe line is inside a library such as ADOdbFix

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.1

2Fix

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.php

Confirmed by php.net

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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-php

2Fix

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-php

3Verify

Repeat the request that used to print the deprecation.

composer show adodb/adodb-php | head -n 5

Confirmed by raw.githubusercontent.com

Did this fix it?

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

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.