• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

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.
0
i-CONICA
Asked:
i-CONICA
3 Solutions
 
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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now