comparing strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

comparing strings

Post by Bill H »

I know that the == operator should not be used to compare strings.
The strcmp() function should be used instead.
But in rather weak-brained moment I wrote:

Code: Select all

<?php
 if ($RowїType] == "GET" && $Type == "NO")
{     // doesn't reall matter what it's doing
}
?>
and it works.

The $Row[Type] is from a database and the $Type is from a $_POST var.
Both strings are extremely limited: will always be NO, GET or TTA.

So, anyway, I'm wondering why it works.
User avatar
jonsyd
Forum Commoner
Posts: 36
Joined: Fri Sep 20, 2002 9:28 am
Location: Oxford, UK

Post by jonsyd »

"==" will work with strings fine, it just doesn't check for type so:

"five" == "five" (TRUE)
5 == 5 (TRUE)
"5" == 5 (TRUE)

I notice in the manual that there is a new comparison operator "===" which checks that both values have the same type as well, could be what you need, so:

"five" === "five" (TRUE)
5 === 5 (TRUE)
"5" === 5 (FALSE)
Post Reply