php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73700 Inconsistent behavior when accessing scalars as an array
Submitted: 2016-12-09 12:31 UTC Modified: 2016-12-09 16:18 UTC
From: onno dot marsman at gmail dot com Assigned:
Status: Duplicate Package: Variables related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: onno dot marsman at gmail dot com
New email:
PHP Version: OS:

 

 [2016-12-09 12:31 UTC] onno dot marsman at gmail dot com
Description:
------------
If you write an array index to a number, a "Cannot use a scalar value as an array" is triggered. This is desired behavior.

In several other similar cases when treating a scalar as an array this warning is not triggered, where this would be desired in my opinion. See test script for the examples.

Because a string is a scalar you would expect this warning to be also triggered when accessing a string as an array, but of course this not desired, since accessing a string as an array is a feature. Therefor the message of the warning "Cannot use a scalar value as an array" maybe should be reconsidered.















Test script:
---------------
// The following case produces a "Cannot use a scalar value as an array" warning:

// 1. write as an array to a number
$a = 1;
$a[1] = 1;

// The following cases do not produce this warning:

// 2. write as an array to null
$a = null;
$a[1] = 1;

// 3. write as an array to false
$a = false;
$a[1] = 1;

// 4. read as an array from a number
$a = 1;
var_dump($a[1]);
// results in NULL

// 5. read as an array from null
$a = null;
var_dump($a[1]);
// results in NULL

// 6. read as an array from false
$a = false;
var_dump($a[1]);
// results in NULL







Expected result:
----------------
Get 6 warnings instead of just 1.

Actual result:
--------------
Only 1 warning.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-09 12:50 UTC] requinix@php.net
-Status: Open +Status: Duplicate
 [2016-12-09 12:50 UTC] requinix@php.net
This is a known issue pending implementation.
https://wiki.php.net/rfc/notice-for-non-valid-array-container
 [2016-12-09 16:18 UTC] onno dot marsman at gmail dot com
-Summary: Incosistent behavior when accessing scalars as an array +Summary: Inconsistent behavior when accessing scalars as an array
 [2016-12-09 16:18 UTC] onno dot marsman at gmail dot com
That is good news, thank you
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC