php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60552 Nonexistent array element is assigned a value
Submitted: 2011-12-17 22:41 UTC Modified: 2011-12-18 03:34 UTC
From: gmtfn at yahoo dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.8 OS:
Private report: No CVE-ID:
 [2011-12-17 22:41 UTC] gmtfn at yahoo dot com
Description:
------------
In the code sample provided, for some reason, $nonexistentElement gets assigned a value of 5. I'd expect this to trigger an error.

Test script:
---------------
$params = array('search'=>'5');
$nonexistentElement = $params['search']['filter'];


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-18 03:30 UTC] anon at anon dot anon
It's an unfortunate quirk but it's not a bug. You're doing string access by character index. The string 'filter' gets parsed as an integer, which yields 0 since it's an invalid number, and the character at index 0 in the string '5' is '5'. See the warning here:
http://www.php.net/manual/en/language.types.string.php#language.types.string.substr
 [2011-12-18 03:34 UTC] laruence@php.net
-Status: Open +Status: Bogus
 [2011-12-18 03:34 UTC] laruence@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

pseudo code:
$params['search']['filter'] 
 => 'search'["fileter"]
   => 'search'[0]
    => 's'
 [2011-12-19 02:33 UTC] gmtfn at yahoo dot com
Thank you both. Because of this, I've switched to using an object in place of an array where I noticed this behavior.

By the way,

The correct pseudo code is:

$params['search']['filter'] 
 => '5'["filter"]
   => '5'[0]
    => '5'
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 18:01:55 2014 UTC