Page 1 of 1

Operators

Posted: Thu Jan 04, 2007 7:22 pm
by Shadowin
When looking through the source code, I've seen several nonexistent operators. My tests show these operators work, but why are they there? "===" and "!==" should be "==" and "!=" respectively. I'm I missing something?

Re: Operators

Posted: Thu Jan 04, 2007 11:20 pm
by garvinhicking
Hi!

PHP has a strict check operation.

"==" and "!=" will perform automatic type conversion automatically. So you can compare "if $string == $int", and if $string is "3" and $int is 3 it will be equal.

However, if you want to compare contents without typecasting (Thus checking variable types) you need to use "===" and "!==", which will strictly compare them. "if $string === $int" would then return false.

Because PHP typecasts strings to booleans when compared with booleans, it is often safer to use === for a comparison, because "if $unset_variable == false" would always return true, even though $unset_variable would not exist. It simply evaluates to false in automatic type conversion.

Hope that clears things up? I have no exact reference to the PHP documentation right now, but there should be a more verbose explanation in there somewhere :)

Best regards,
Garvin

Cleared Up

Posted: Thu Jan 04, 2007 11:33 pm
by Shadowin
Thanks for clearing that up. I've learned to refer to the PHP.net documents rather than W3Schools (incomplete info on the operators).

I'm actually a professional C# developer. PHP is more of a hobby thing for me.

Re: Cleared Up

Posted: Thu Jan 04, 2007 11:40 pm
by garvinhicking
Hi!

Sure, the === operator is one of the some bizzare things that PHP has to offer. :)

The php.net site is quite good to learn the basics, I'd definitely suggest to look into that for future reference.

Coming from C# you will definitely find some oddities in PHP, but PHP still has a few easier things to offer in terms of web development that you might enjoy. :-)

Best regards and have fun,
Garvin