Solved

php string comparison, why the same?

Posted on 2014-12-23
9
106 Views
Last Modified: 2014-12-23
Hi all,

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>

Open in new window


And this is what I have in my browser:
033 -- +33
The strings are equal.

Open in new window


My version of PHP is 5.2.17
0
Comment
Question by:Richard Coffre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 12

Expert Comment

by:FarWest
ID: 40515050
it looks like treating '+' as 0

I think you can do work around by comparing sha1 output
if (strcmp(sha1($sub1), sha1($sub2)) !== 0) {
	echo "The strings are equal.<br>";
} else {
	echo "The strings are not equal.<br>";
}

Open in new window

0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40515053
Because you're echoing "The strings are equal.<br>" when they are not!
Change the !== to == in line 17.
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 40515101
Oups,

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>

Open in new window


The output is:
033 -- +33
The strings are equal.

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40515139
Use === for a type and value comparison else PHP will just see them as numbers.
i.e.
033 becomes 33
+33 becomes 33

Using === 033 and +33 are compared exactly as that.

if ($sub1 === $sub2) {
...

Open in new window

0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40515153
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:
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.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40515166
Congratulations for repeating what I already said
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40515944
@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!
0
 
LVL 58

Expert Comment

by:Gary
ID: 40515997
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

Anyway, I'll try to send you some congratulations next time when I see you posting similar answers following others
Invariably (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.
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40516068
unless it adds something else to the discussion/answer
Yes, that's why I didn't delete mine.  I thought an explanation from PHP official site would be worth referencing.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question