Take the State of PHP 2026 Survey →

Disable autovivification on false View as Markdown

Accepted
Authors
Kamil Tekiela dharman@php.net
Date
2021-05-25
Implementation
https://github.com/php/php-src/pull/7131
Source
wiki.php.net/rfc/autovivification_false

Vote

Deprecate autovivification from false? closed
Yes
34
No
2
36 voters
alcaeus Yes 2021-06-14 13:24
asgrim Yes 2021-06-15 13:52
ashnazg Yes 2021-06-16 14:05
bmajdak Yes 2021-06-09 20:31
brzuchal Yes 2021-06-10 03:49
bwoebi Yes 2021-06-11 09:12
cschneid Yes 2021-06-09 21:15
danack No 2021-06-11 12:03
derick Yes 2021-06-09 19:59
dharman Yes 2021-06-23 21:09
dmitry No 2021-06-11 06:46
ekin Yes 2021-06-19 13:34
galvao Yes 2021-06-09 20:42
ilutov Yes 2021-06-09 22:44
kalle Yes 2021-06-10 04:08
kguest Yes 2021-06-11 15:42
kocsismate Yes 2021-06-09 20:29
levim Yes 2021-06-16 01:45
lufei Yes 2021-06-17 07:29
marandall Yes 2021-06-09 21:17
mcmic Yes 2021-06-10 07:29
nicolasgrekas Yes 2021-06-11 07:03
nikic Yes 2021-06-10 12:09
ocramius Yes 2021-06-09 21:22
patrickallaert Yes 2021-06-11 16:52
pierrick Yes 2021-06-10 04:19
pollita Yes 2021-06-12 15:00
ramsey Yes 2021-06-10 18:37
reywob Yes 2021-06-10 16:08
santiagolizardo Yes 2021-06-16 20:44
sebastian Yes 2021-06-10 05:11
sergey Yes 2021-06-16 09:31
tandre Yes 2021-06-10 23:25
theodorejb Yes 2021-06-09 20:09
trowski Yes 2021-06-10 18:44
wyrihaximus Yes 2021-06-11 12:05

Discussions on the mailing list

Disable autovivification on false
by Kamil Tekiela · 11 messages
[VOTE] Deprecate autovivification on false
by Kamil Tekiela · 7 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:50 UTC

Introduction

PHP natively allows for autovivification (auto-creation of arrays from falsey values). This feature is very useful and used in a lot of PHP projects, especially if the variable is undefined. However, there is a little oddity that allows creating an array from a false and null value.

// From undefined
$arr[] = 'some value';
$arr['doesNotExist'][] = 2;
// From false
$arr = false;
$arr[] = 2;
// From null
$arr = null;
$arr[] = 2;

This RFC talks only about autovivification from false.

Proposal

The proposal is to disallow autovivification from false values. We will continue to allow autovivification from undefined variable and null, but in every other case an error message will be shown:

Cannot use a scalar value as an array

In PHP 8.1, appending to a variable of type false will display a deprecation notice. For example:

Deprecated: Automatic conversion of false to array is deprecated in

In PHP 9.0, this will throw a fatal error, the same as for other scalar types.

Rationale

Historically, many functions return false on error or array on success. In some scenarios, developers might want to assign an additional element to an array, e.g. a total section at the end of array fetched from PDO. PHP does not complain if the value is false. Whether it should be considered a bug or a “feature” is up for debate.

$stmt = $pdo->query("SELECT 1 WHERE 0");
$arr = $stmt->fetch();
// naively try to append
$arr[] = ['new row values'];

Throwing an error in such cases could save developers from common bugs in their code. On the other hand, this feature could also be considered useful by some developers. The rationale here is that the behaviour is inconsistent. Neither true, 0, nor “” can be used in autovivification today, so does it make sense to allow false?

Allowing autovivification on null can be a sign of a buggy code, but it is less of a problem than false. In PHP, null and undefined are very similar and often treated the same way, e.g. isset() and ??.

Backward Incompatible Changes

Every single usage autovivification on false will throw a deprecation error in PHP 8.1 and throw a fatal error in PHP 9.0.

Vote

Should we deprecate autovivification from false?

Started: 2021-06-09T20:00:00Z Ends: 2021-06-23T20:00:00Z

Vote: Deprecate autovivification from false? — Yes: 34 · No: 2 (closed)

Discussion on internals

https://externals.io/message/114595