Bug #73700 [NEW]: Incosistent behavior when accessing scalars as an array
| From: | onno dot marsman at gmail dot com | Date: | Fri, 09 Dec 2016 12:31:33 +0000 |
| Subject: | Bug #73700 [NEW]: Incosistent behavior when accessing scalars as an array | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-205876@lists.php.net to get a copy of this message | ||
From: onno dot marsman at gmail dot com
Operating system:
PHP version: Irrelevant
Package: Variables related
Bug Type: Bug
Bug description:Incosistent behavior when accessing scalars as an array
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.
--
Edit bug report at https://bugs.php.net/bug.php?id=73700&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=73700&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=73700&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=73700&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=73700&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=73700&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=73700&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=73700&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=73700&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=73700&r=support
Expected behavior: https://bugs.php.net/fix.php?id=73700&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=73700&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=73700&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=73700&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73700&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=73700&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=73700&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=73700&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=73700&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=73700&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=73700&r=mysqlcfg