note 99254 added to language.operators.comparison
| From: | arnaud at arnapou dot net | Date: | Fri, 06 Aug 2010 08:12:31 +0000 |
| Subject: | note 99254 added to language.operators.comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171421@lists.php.net to get a copy of this message | ||
I discover after 10 years of PHP development something awfull : even if you make a string comparison
(both are strings), strings are tested like integers and leading "space" character (even
\n, \r, \t) is ignored ....
I spent hours because of leading \n in a string ... it hurts my developper sensibility to see two
strings beeing compared like integers and not like strings ... I use strcmp now for string
comparison ... so stupid ...
Test code :
<?php
test("1234", "1234");
test("1234", " 1234");
test("1234", "\n1234");
test("1234", "1234 ");
test("1234", "1234\n");
function test($v1, $v2) {
echo "<h1>[".show_cr($v1)."] vs [".show_cr($v2)."]</h1>";
echo my_var_dump($v1)."<br />";
echo my_var_dump($v2)."<br />";
if($v1 == $v2) {
echo "EQUAL !";
}
else {
echo "DIFFERENT !";
}
}
function show_cr($var) {
return str_replace("\n", "\\n", $var);
}
function my_var_dump($var) {
ob_start();
var_dump($var);
$dump = show_cr(trim(ob_get_contents()));
ob_end_clean();
return $dump;
}
?>
Displays this ->
[1234] vs [1234]
string(4) "1234"
string(4) "1234"
EQUAL !
[1234] vs [ 1234]
string(4) "1234"
string(5) " 1234"
EQUAL !
[1234] vs [\n1234]
string(4) "1234"
string(5) "\n1234"
EQUAL !
[1234] vs [1234 ]
string(4) "1234"
string(5) "1234 "
DIFFERENT !
[1234] vs [1234\n]
string(4) "1234"
string(5) "1234\n"
DIFFERENT !
----
Server IP: 195.221.21.36
Probable Submitter: 80.13.111.8
----
Manual Page -- http://www.php.net/manual/en/language.operators.comparison.php
Edit -- https://master.php.net/note/edit/99254
Del: integrated -- https://master.php.net/note/delete/99254/integrated
Del: useless -- https://master.php.net/note/delete/99254/useless
Del: bad code -- https://master.php.net/note/delete/99254/bad+code
Del: spam -- https://master.php.net/note/delete/99254/spam
Del: non-english -- https://master.php.net/note/delete/99254/non-english
Del: in docs -- https://master.php.net/note/delete/99254/in+docs
Del: other reasons-- https://master.php.net/note/delete/99254
Reject -- https://master.php.net/note/reject/99254
Search -- https://master.php.net/manage/user-notes.php