ID:               35713
 User updated by:  nalkat at yahoo dot com
 Reported By:      nalkat at yahoo dot com
 Status:           Open
 Bug Type:         Scripting Engine problem
 Operating System: Linux
 PHP Version:      5.1.1
 New Comment:

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?


Previous Comments:
------------------------------------------------------------------------

[2005-12-16 19:56:08] 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:49:27] [EMAIL PROTECTED]

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:32:50] 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 17:51:48] [EMAIL PROTECTED]

Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.



------------------------------------------------------------------------

[2005-12-16 17:30:38] 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"
}



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=35713&edit=1

Reply via email to