It seems that PHP's ===
operator is case sensitive? So is there any reason to use strcmp()
? Is it safe to do something like:
if ( $password === $password2 ) { ... }
It seems that PHP's ===
operator is case sensitive? So is there any reason to use strcmp()
? Is it safe to do something like:
if ( $password === $password2 ) { ... }
You can use
strcmp()
if you wish to order/compare strings lexicographically. If you just wish to check for equality then==
is just fine.The reason to use it is because
strcmp
===
only returnstrue
orfalse
, it doesn't tell you which is the "greater" string.Always remember, when comparing strings, you should use
===
operator (strict comparison) and not==
operator (loose comparison).strcmp will return different values based on the environment it is running(Linux/Windows)!
The reason is the that it has a bug as the bug report says https://bugs.php.net/bug.php?id=53999
Please handle with care!!Thank you.
You should never use
==
for string comparison.===
is OK.Just run the above code and you'll see why.
Now, that's a little better.
Don't use
==
in PHP. It will not do what you expect. Even if you are comparing strings to strings, PHP will implicitly cast them to floats and do a numerical comparison if they appear numerical.For example
'1e3' == '1000'
returns true. You should use===
instead.