String Comparison Gotcha in PHP!

Can you expect a mature language that has been around for 13 years, and powering many web sites to do basic string comparison properly? No, not really!!

$x[0] = "10000000000000000000001";
$x[1] = "11011000000000000010101";
$x[2] = "10101010101010101010101";

$toFind = "11011000000000000010001";
$k = array_search($toFind, $x);
echo "match is $k";

$p = ($toFind == $x[1]);
echo "match is $p";

Run the above code and see for yourself! It will match $toFind with $x[1]!! I got hit with this nasty surprise when calling the array_search() function for a programming contest problem. It appears like PHP is truncating these strings, which it thinks are numbers and comparing the first few characters. Can anyone give a logical explanation of what’s going on? Am I doing something fundamentally wrong, or is PHP hosed?

Update: Jude pointed that using “strict” flag in array_search() uses “===”, and hence gets rid of the problem. But the fundamental question still remains. Why would PHP do an automatic type cast to integer while comparing two strings?

Update 2: The fundamental issue can be traced to this single statement in PHP documentation – “If you compare two numerical strings, they are compared as integers.” This was the root of all problems. I don’t like that behavior, but hey this is PHP. I have a similar gripe with Tcl as well. Try this:

set x "072"
puts [incr $x]
set y "09"
puts [incr y]

It will print 59, followed by an error ‘expected integer but got “09” (looks like invalid octal number)’ as Tcl treats any string starting with 0 as octal – a tradition it borrows from C, but unfortunately, it overloads the string type!

2 Replies to “String Comparison Gotcha in PHP!

Leave a Reply

Your email address will not be published. Required fields are marked *