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

From: Date: Fri, 30 Dec 2016 15:58:12 +0000
Subject: Bug #72786 [Opn->Dup]: Inconsistent behavior when ArrayDimFetch operates on non-array containers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206244@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72786&edit=1 ID: 72786 Updated by: cmb@php.net Reported by: php at abiusx dot com Summary: Inconsistent behavior when ArrayDimFetch operates on non-array containers -Status: Open +Status: Duplicate Type: Bug Package: Arrays related Operating System: OS X 10.11 PHP Version: 7.0.9 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: Duplicate of bug #37676. Previous Comments: ------------------------------------------------------------------------ [2016-08-08 17:13:21] php at abiusx dot com 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 this bug report at https://bugs.php.net/bug.php?id=72786&edit=1

« previous php.bugs (#206244) next »