Bug #51412 [Com]: inconsistency using uninitialized array elements
| From: | zingus at gmail dot com | Date: | Sat, 27 Mar 2010 14:41:22 +0000 |
| Subject: | Bug #51412 [Com]: inconsistency using uninitialized array elements | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-149101@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=51412&edit=1
ID: 51412
Comment by: zingus at gmail dot com
Reported by: looris at gmail dot com
Summary: inconsistency using uninitialized array elements
Status: Bogus
Type: Bug
Package: Arrays related
Operating System: linux, osx, (any?)
PHP Version: Irrelevant
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2010-03-27 15:07:05] 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:00:22] 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] =>
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51412&edit=1