Operators

Discussion corner for Developers of Serendipity.
Post Reply
Shadowin
Regular
Posts: 6
Joined: Thu Jan 04, 2007 4:40 am
Location: Columbia, SC, USA
Contact:

Operators

Post 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?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Operators

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Shadowin
Regular
Posts: 6
Joined: Thu Jan 04, 2007 4:40 am
Location: Columbia, SC, USA
Contact:

Cleared Up

Post 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.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Cleared Up

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply