Richard Coffre
asked on
php string comparison, why the same?
Hi all,
When I test the string comparison functions, I do not understand why they are equal.
This is the script:
And this is what I have in my browser:
My version of PHP is 5.2.17
When I test the string comparison functions, I do not understand why they are equal.
This is the script:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Comparaison de chaînes de caractères</title>
</head>
<body>
<?php
$str1 = "033123456789";
$str2 = "+33456789087";
$sub1 = substr($str1, 0, 3);
$sub2 = substr($str2, 0, 3);
echo $sub1 . " -- " . $sub2 . "<br>";
if (strcmp($sub1, $sub2) !== 0) {
echo "The strings are equal.<br>";
} else {
echo "The strings are not equal.<br>";
}
?>
</body>
</html>
And this is what I have in my browser:
033 -- +33
The strings are equal.
My version of PHP is 5.2.17
Because you're echoing "The strings are equal.<br>" when they are not!
Change the !== to == in line 17.
Change the !== to == in line 17.
ASKER
Oups,
The weird code is:
The output is:
The weird code is:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Comparaison de chaînes de caractères</title>
</head>
<body>
<?php
$str1 = "033123456789";
$str2 = "+33456789087";
$sub1 = substr($str1, 0, 3);
$sub2 = substr($str2, 0, 3);
echo $sub1 . " -- " . $sub2 . "<br>";
if ($sub1 == $sub2) {
echo "The strings are equal.<br>";
} else {
echo "The strings are not equal.<br>";
}
?>
</body>
</html>
The output is:
033 -- +33
The strings are equal.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
According to http://php.net/manual/en/language.operators.comparison.php, when using "==", it evaluates to TRUE if $a is equal to $b after type juggling, meaning that:
So you should use either strcmp() or the identical operator "===" to compare possible numerical strings.
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
So you should use either strcmp() or the identical operator "===" to compare possible numerical strings.
Congratulations for repeating what I already said
@Gary,
Come on! I think you've seen some of my comments to other questions. I don't repeat what other people already said.
And you have posted some comments regarding that people may have opened the page, enter their comments, but didn't realize that their answers are very similar to others' until they hit the Submit button.
Plus that if it were not for my previous comment (45 minutes before yours), the author would not even know he had pasted the wrong script.
Anyway, I'll try to send you some congratulations next time when I see you posting similar answers following others. ^^
Cheers!
Come on! I think you've seen some of my comments to other questions. I don't repeat what other people already said.
And you have posted some comments regarding that people may have opened the page, enter their comments, but didn't realize that their answers are very similar to others' until they hit the Submit button.
Plus that if it were not for my previous comment (45 minutes before yours), the author would not even know he had pasted the wrong script.
Anyway, I'll try to send you some congratulations next time when I see you posting similar answers following others. ^^
Cheers!
My answer was based on his real code.
You repeated the exact same analogy of my comment
If someone comes up with the same answer within a few minutes then I won't bitch about it unless it is a 10 word comment that consists of a link and copy/paste which your comment barely passes
The author posting the wrong code is irrelevant - many times authors post a synopsis of their code that is irrelevant as they have removed the important parts or thought this 3 line version of their code is enough.
You repeated the exact same analogy of my comment
If someone comes up with the same answer within a few minutes then I won't bitch about it unless it is a 10 word comment that consists of a link and copy/paste which your comment barely passes
Anyway, I'll try to send you some congratulations next time when I see you posting similar answers following othersInvariably (99%) where I post a similar comment to what has already been posted in the time it took to write the comment then I will delete unless it adds something else to the discussion/answer
The author posting the wrong code is irrelevant - many times authors post a synopsis of their code that is irrelevant as they have removed the important parts or thought this 3 line version of their code is enough.
unless it adds something else to the discussion/answerYes, that's why I didn't delete mine. I thought an explanation from PHP official site would be worth referencing.
I think you can do work around by comparing sha1 output
Open in new window