php.net |  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: looris at gmail dot com
New email:
PHP Version: OS:

 

 [2010-03-27 15:00 UTC] looris at gmail dot com
Description:
------------
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:
---------------
$pitale=array();
$pitale["ok"]++;
$pitale["bug"]--;
print_r($pitale);

Expected result:
----------------
Array
(
    [ok] => 1
    [bug] => -1
)

---OR---

Array
(
    [ok] => 
    [bug] => 
)


Actual result:
--------------
Array
(
    [ok] => 1
    [bug] => 
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-27 15:07 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-03-27 15:07 UTC] johannes@php.net
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. 
    http://php.net/manual/en/language.operators.increment.php

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)

<?php
$a=array();
$b=array();
$c=null;
$d=null;

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

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

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

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

Result:
-------

$a['z']--;
array (
  'z' => NULL,
)

$b['z']+=-1;
array (
  'z' => -1,
)

$c-=1;
-1

$d--;
NULL
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 15:01:32 2024 UTC