php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35713 cannot use isset or other on variables indexed by a numerical string
Submitted: 2005-12-16 17:30 UTC Modified: 2005-12-16 21:10 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: nalkat at yahoo dot com Assigned: tony2001 (profile)
Status: Closed Package: CGI/CLI related
PHP Version: 5.1.1 OS: Linux
Private report: No CVE-ID: None
 [2005-12-16 17:30 UTC] nalkat at yahoo dot com
Description:
------------
When dealing with arrays indexed by a numerical string (i.e. $array['1']) and then attempting to use isset or similar on the value, results are not as expected.

As you can see from the example below, it would be impossible to use something such as:

$opts = getopt("1:2:a:b:");
if (isset($opts['1'])) {
  do_something();
}
if (isset($opts['2'])) {
  do_something_else();
}
if (isset($opts['a'])) {
  echo "works\n";
}
...
...

Reproduce code:
---------------
<?

// test.php

$opts = getopt("1:2:3:4:a:");
if (isset($opts['1'])) {
        echo "it worked for 1!\n";
}
if (isset($opts['a'])) {
        echo "it worked for a!\n";
}
var_dump ($opts);
?>

(host)# php -q test.php -1 hi -a bye

Expected result:
----------------
EXPECTED:
-----------
it worked for 1!
it worked for a!
array(2) {
  ["1"]=>
  string(2) "hi"
  ["a"]=>
  string(3) "bye"
}


Actual result:
--------------
ACTUAL:
-----------
it worked for a!
array(2) {
  ["1"]=>
  string(2) "hi"
  ["a"]=>
  string(3) "bye"
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-12-16 19:32 UTC] nalkat at yahoo dot com
This is definately a bug in PHP itself.  I would be willing to bet that no matter where you attempted this code snippet, it will still not work (CLI/CGI/Web/etc).

If there is further information that you require to get this resolved, I would be more than happy to help.
 [2005-12-16 19:49 UTC] tony2001@php.net
This code works perfectly fine:
$array = array(1=>1);
var_dump(isset($array["1"]));
var_dump(isset($array[1]));

The problem is that getopt() adds array elements with numeric indexes as strings.
 [2005-12-16 19:56 UTC] nalkat at yahoo dot com
I dont think its a problem of getopt adding numerical string indexes, because you can create an array with a numerical string index value, and you will still have the same problem.

If what you say truely is the case, then it is still a problem with the PHP core itself allowing this action.

I think the real problem here is that when you are attempting to access an array element with a numerical string as the index value, PHP is not seeing it as a string when enclosed in quotes, but rather it is converting the numerical string to a numerical literal.
 [2005-12-16 19:59 UTC] nalkat at yahoo dot com
and if you will notice, from your example code snippet, you are not doing a test on the value of the array element. You are merely assigning it.  Attempt to do some kind of validation...  does it work?
 [2005-12-16 20:04 UTC] tony2001@php.net
>you can create an array with a numerical string index
> value, and you will still have the same problem.
Eh? What are you talking about?

>and if you will notice, from your example code snippet, 
>you are not doing a test on the value of the array element.
Did you try to execute it?
What kind of notice you get with this code?
<?php
$array = array(1=>1);
var_dump(isset($array["1"]));
var_dump(isset($array[1]));
?>
 [2005-12-16 20:33 UTC] nalkat at yahoo dot com
Tony:

Ok this is what I am seeing.  When I attempt to assign the numerical string index to the array manually PHP converts that numerical value to an actual number.  

It would appear that you are correct that getopt is causing the problem, however, I still feel that it is a PHP core problem that is allowing this behavior to occur.

try this snippet:

<?php
$array = array();
$array['1'] = "blah";
if (isset($array['1'])) {
        echo "the test condition worked\n";
}
var_dump($array);
unset ($array);


$array = getopt("1:");
if (isset($array['1'])) {
        echo "the test condition worked\n";
}
var_dump($array);
?>

You will notice that the test condition fails when the variable is being assigned by getopt, but not when it is assigned manually.  You will also notice in the output of the var_dumps that the manual assignment converts the numerical string to an actual number whereas the getopt version does not.

You tell me.. is this a PHP problem of allowing the numerical string index to be assigned or merely a getopt problem where this is actually occuring?

At anyrate, should I be able to use getopt with a numerical command-line argument?  Is there some kind of work around?
 [2005-12-16 20:45 UTC] tony2001@php.net
So, the problem exists only in CLI.
I need to test the patch..
 [2005-12-16 21:10 UTC] tony2001@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC