|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2016-12-30 15:58 UTC] cmb@php.net
-Status: Open
+Status: Duplicate
-Assigned To:
+Assigned To: cmb
[2016-12-30 15:58 UTC] cmb@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 21:00:01 2025 UTC |
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