|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51412 inconsistency using uninitialized array elements
Submitted: 2010-03-27 15:00 UTC Modified: 2010-03-27 15:07 UTC
From: looris at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: Irrelevant OS: linux, osx, (any?)
Private report: No CVE-ID: None
 [2010-03-27 15:00 UTC] looris at gmail dot com
While I agree that it would be better to actually initialize elements before using them, it is anyway wrong that they behave in inconsistent ways when you do that.

They should BOTH count as 0, hence become 1 and -1, OR they should **BOTH** stay NULL.

It does not make any sense at all to have them behave in two different ways.

Test script:

Expected result:
    [ok] => 1
    [bug] => -1


    [ok] => 
    [bug] => 

Actual result:
    [ok] => 1
    [bug] => 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-27 15:07 UTC]
-Status: Open +Status: Bogus
 [2010-03-27 15:07 UTC]
The documentation claims

    The increment/decrement operators do not affect
    boolean values. Decrementing NULL values has no
    effect too, but incrementing them results in 1.

This might be considered inconsistent but changing this would be a rather random compatibility break.
 [2010-03-27 15:41 UTC] zingus at gmail dot com
Another script, further highlighting the inconsistence, and the fact it isn't an array bug, but one of unary decrease operator (or of the += and -= operators)


echo "\$a['z']--;\n";
var_export($a); print "\n\n";

echo "\$b['z']+=-1;\n";
var_export($b); print "\n\n";

echo "\$c-=1;\n";
var_export($c); print "\n\n";

echo "\$d--;\n";
var_export($d); print "\n\n";


array (
  'z' => NULL,

array (
  'z' => -1,


PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Apr 15 06:01:24 2021 UTC