Date: 09/21/98
- Next message: Colin Viebrock: "Re: [PHP3] ODBC Fehler"
- Previous message: Colin Viebrock: "Re: [PHP3] Expandable Menus?"
- Next in thread: Zeev Suraski: "Re: [PHP3] (bug?) ksort() and implicit typing of variables."
- Reply: Zeev Suraski: "Re: [PHP3] (bug?) ksort() and implicit typing of variables."
- Maybe reply: Richard Lynch: "Re: [PHP3] (bug?) ksort() and implicit typing of variables."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
I've been bitten by an unexpected bug in one big PHP3 script of mine and
I
finally narrowed it down to an unexpected behaviour (for me) of ksort
with regard to variable types.
The issue is, I fill my table with keys and values, but ksort just seems
to decide that
some keys are integers and others are strings, which somewhat messes the
sort results.
Here is a short example of what I get:
Short script is:
<?
$toto["001"]="a";
$toto["010"]="c";
$toto[(string)"100"]="d";
$toto["002"]="b";
$toto["110"]="e";
ksort($toto);
for(reset($toto); $test = key($toto); next($toto)) {
echo ($test." (".gettype($test).") : ".$toto[$test]." <BR>\n");
};
?>
<P>==========<P>
<? // another go at it.
reset($toto);
for(ksort($toto); $test = key($toto); next($toto)) {
echo ($test." (".gettype($test).") : ".$toto[$test]." <BR>\n");
};
?>
Result is:
100 (integer) : d <BR>
110 (integer) : e <BR>
001 (string) : a <BR>
002 (string) : b <BR>
010 (string) : c <BR>
<P>==========<P>
100 (integer) : d <BR>
110 (integer) : e <BR>
001 (string) : a <BR>
002 (string) : b <BR>
010 (string) : c <BR>
Expected result would of course have been this order: 001 002 010 100
110
PHP just decides that some keys are integers, when I'd like them all to
be
strings. Notice that even if I try to set the type explicitly to
strings,
nothing changes. If I set them explicitly to integers, then I get keys
of 1, 2, 10, 100, 110 which are useless for the rest of my script.
Other stuff I tried which didn't help:
ksort((string)$toto);
(string)ksort($toto);
Even now that I'm aware of the issue, I really don't know how it can be
handled in a clean and practical way from the php3-scripter's end.
I have some difficulties understanding what the bug might be, however.
Maybe it would be enough for ksort to output strings before integers.
Maybe ksort should not be allowed to mix up variable types.
Or maybe the arrays should "remember" the type of variable when the key
is
defined (so I could just always write $toto[(string)key]=value ). In
this
case, nothing needs to be changed to ksort.
Or we just need an optionnal parameter to sorting functions, like so:
ksort($toto, "string");
Or we need the kusort($toto,) function, like we have usort() already
with the
following compare function:
function cmp($a,$b) {if($a==$b) return 0;return ((string)$a>(string)$b)
? 1:-1;}
(I'm concerned about the performance overhead, these are not 3-digit
keys
for nothing).
I checked in the bug database but there is nothing with ksort in the
description.
I submitted it, in the hope that I'm not missing some simple obvious
more
elegant way of sorting these keys. (it's
http://ca.php.net/bugs.php3?id=764 )
Linux
php3.0.3
./configure --with-apache=/home/www/apache_1.3.0 --with-mysql=/usr
--with-config-file-path=/home/www/apache_1.3.0/etc
--with-exec-dir=/home/www/safe-bin
--enable-track-vars=yes --enable-magic-quotes=yes --enable-bcmath=yes
It took me a lot of time to figure out what was wrong (I debugged the
script
with small values of the key), so I hope this can be of help to someone,
even if the bug does not get corrected right now (I found a kludge for
my
script, but it's script-specific, so not very elegant).
Cheers,
Jean-Marc Libs
-- PHP 3 Mailing List http://www.php.net/ To unsubscribe send an empty message to php3-unsubscribe <email protected> To subscribe to the digest list: php3-digest-subscribe <email protected> For help: php3-help <email protected> Archive: http://www.php.net/mailsearch.php3
- Next message: Colin Viebrock: "Re: [PHP3] ODBC Fehler"
- Previous message: Colin Viebrock: "Re: [PHP3] Expandable Menus?"
- Next in thread: Zeev Suraski: "Re: [PHP3] (bug?) ksort() and implicit typing of variables."
- Reply: Zeev Suraski: "Re: [PHP3] (bug?) ksort() and implicit typing of variables."
- Maybe reply: Richard Lynch: "Re: [PHP3] (bug?) ksort() and implicit typing of variables."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

