Bug #72786 [NEW]: Inconsistent behavior when ArrayDimFetch operates on non-array containers

From: Date: Mon, 08 Aug 2016 17:13:25 +0000
Subject: Bug #72786 [NEW]: Inconsistent behavior when ArrayDimFetch operates on non-array containers
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203093@lists.php.net to get a copy of this message
From: php at abiusx dot com Operating system: OS X 10.11 PHP version: 7.0.9 Package: Arrays related Bug Type: Bug Bug description:Inconsistent behavior when ArrayDimFetch operates on non-array containers Description: ------------ Using arraydimfetch (i.e. [] operator) on non-array containers (e.g. int, null, bool, float) should produce at least NOTICE level error. However, it does generate NOTICE on some cases, and does nothing on other cases. Also, using the same arraydimfetch on the same container, using AssignByRef (i.e. =& operator) will inconsistently convert the container to array and create the element in some cases! This issue is a generalization of the following bug reports: 39915 37676 48560 64194 65484 72636 All the worse, the inconsistent behavior itself is not consistent between different PHP versions, chiefly because each bug report tackles one part of the problem and solves it. Current Results: All containers through warnings, and do not modify the original variable. As for null, not only no warning is through, when accessing byref, the null is converted into an array and the index is created! This might be because PHP treats nulls as undefined variables in some cases, and thus thinks this array is a new variable. Test script: --------------- <?php $int=5; $bool=true; $null=null; $float=5.1; $index_v=5; $index_r=6; echo "Int:\n"; $int_v=$int[$index_v]; $int_r=&$int[$index_r]; var_dump($int,$int_v,$int_r); echo str_repeat("-",80),PHP_EOL; echo "Bool:\n"; $bool_v=$bool[$index_v]; $bool_r=&$bool[$index_r]; var_dump($bool,$bool_v,$bool_r); echo str_repeat("-",80),PHP_EOL; echo "Null:\n"; $null_v=$null[$index_v]; $null_r=&$null[$index_r]; var_dump($null,$null_v,$null_r); echo str_repeat("-",80),PHP_EOL; echo "Float:\n"; $float_v=$float[$index_v]; $float_r=&$float[$index_r]; var_dump($float,$float_v,$float_r); Expected result: ---------------- Same behavior for all containers, specifically null should not behave so magically. Actual result: -------------- Int: Warning: Cannot use a scalar value as an array in sample-arraydimref.php on line 12 Notice: Undefined variable: int_r in sample-arraydimref.php on line 13 int(5) NULL NULL -------------------------------------------------------------------------------- Bool: Warning: Cannot use a scalar value as an array in sample-arraydimref.php on line 18 Notice: Undefined variable: bool_r in sample-arraydimref.php on line 19 bool(true) NULL NULL -------------------------------------------------------------------------------- Null: array(1) { [6]=> &NULL } NULL NULL -------------------------------------------------------------------------------- Float: Warning: Cannot use a scalar value as an array in sample-arraydimref.php on line 31 Notice: Undefined variable: float_r in sample-arraydimref.php on line 32 float(5.1) NULL NULL -- Edit bug report at https://bugs.php.net/bug.php?id=72786&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72786&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72786&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72786&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=72786&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=72786&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=72786&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=72786&r=needscript Try newer version: https://bugs.php.net/fix.php?id=72786&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=72786&r=support Expected behavior: https://bugs.php.net/fix.php?id=72786&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=72786&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=72786&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=72786&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72786&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=72786&r=dst IIS Stability: https://bugs.php.net/fix.php?id=72786&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=72786&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=72786&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=72786&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=72786&r=mysqlcfg

« previous php.bugs (#203093) next »