php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41195 Indexing non-array should raise notice
Submitted: 2007-04-26 01:38 UTC Modified: 2007-08-17 09:25 UTC
From: lbayuk at pobox dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.1 OS: Linux
Private report: No CVE-ID: None
 [2007-04-26 01:38 UTC] lbayuk at pobox dot com
Description:
------------
If I index a scalar or NULL variable as if it were an array, I get NULL back with no warning or notice. This is true for any index value, including 0.  I expected an Undefined offset or incorrect type message.  The manual says an integer variable = n will convert to array(0=>$n); that works on casting to array but not on indexing.


Reproduce code:
---------------
<?php
error_reporting(E_ALL+E_STRICT);
$arry = array(1=>1);
$notarry = NULL;
$intvar = 123;
echo "1. arry[1] = " . $arry[1] . "\n";
echo "2. arry[2] = " . $arry[2] . "\n";
echo "3. intvar[0] = " . $intvar[0] . "\n";
echo "4. intvar[1] = " . $intvar[1] . "\n";
echo "5. notarry[1] = " . $notarry[1] . "\n";


Expected result:
----------------
"Notice: Undefined offset" (or incorrect type) for (2), (3), (4), and (5). Or maybe (3) should echo "123" via implicit cast. But only (2) raises a Notice; the others just echo NULL. 


Actual result:
--------------
1. arry[1] = 1

Notice: Undefined offset:  2 in /a5/home/ljb/lang/php/p.php on line 7
2. arry[2] = 
3. intvar[0] = 
4. intvar[1] =
5. notarry[1] =


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-01 01:32 UTC] lbayuk at pobox dot com
Changed category... I thought I put it in as "Arrays related", but I guess I missed and it ended up as "Feature/Change Request". To me, this is either a bug, or a documentation improvement request, not a feature request.
 [2007-05-02 10:35 UTC] tony2001@php.net
Change requests belong to the "Feature/Change Request" category.
Reclassified.
 [2007-05-03 01:09 UTC] lbayuk at pobox dot com
OK, so we're back at Feature/Change Request. Except on further thought I like the way it works and would prefer it not change. I just want clarification of what is supposed to happen when you apply square brackets to a non-array.

If it is working as intended (silenty returns NULL), please document it.
If it isn't working as intended, please fix it.
 [2007-05-03 10:30 UTC] tony2001@php.net
Surely it is intended.
 [2007-08-17 09:25 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"Accessing by [] or {} to variables of other type silently returns NULL."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC