Page 1 of 1

comparing strings

Posted: Mon Nov 25, 2002 8:53 am
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.

Posted: Mon Nov 25, 2002 11:49 am
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)