|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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"
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
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?