php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72786 Inconsistent behavior when ArrayDimFetch operates on non-array containers
Submitted: 2016-08-08 17:13 UTC Modified: 2016-12-30 15:58 UTC
From: php at abiusx dot com Assigned: cmb (profile)
Status: Duplicate Package: Arrays related
PHP Version: 7.0.9 OS: OS X 10.11
Private report: No CVE-ID: None
 [2016-08-08 17:13 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [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
Duplicate of bug #37676.
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Mon Nov 19 11:01:25 2018 UTC