Is (true === $var) the same as ($var === true)?

Hi,

I've read that it's generally better to use if(true === $var) rather than just if($var) for various reasons.

But I've seen it as (true === $var) when It would appear more logical to me to do ($var === true).

Is there a difference?

Thanks.
LVL 6
i-CONICAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shahzad Fateh AliWeb Solutions Architect & Technical Project Manager- VentureDive (Pvt) LtdCommented:
No, there are not difference in both statements, but it depends of the understanding for the programmer that how he/she wants to write it.
0
john-formbyCommented:
No, those statements are interchangable.
0
respondifyCommented:
There is no difference, "===" instead of "==" just compares the type as well as the value of the variables.

For more information, see http://se2.php.net/manual/en/language.operators.comparison.php

Hope this helps.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

jet-blackCommented:
Actually it is programmers' technique to eliminate coding mistakes.

In example:

Lets say you are checking if $var is equal to true.
Then you write:
if ($var == true)
{
...
}

Right, but if you forget one "=", your condition becomes:
if ($var = true)
{
...
}
which returns always true because you are setting the $var value to true  and you will not be able to correct it.
However if you want to write

if (true == $var)
{
...
}
and you forget one "=", then your condition becomes:
if (true = $var)
{
...
}
and you will be shown an error and you will be able to correct it.

Thats the idea.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Avinash ZalaWeb ExpertCommented:
== will compare only values

and

=== will compare value as well as data type.


Hope this helps.
Addy
0
i-CONICAAuthor Commented:
Thanks guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.