Operators
Operators
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
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
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/
# 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/
Cleared Up
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.
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
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
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/
# 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/