Comparator difference in php

Asked

Viewed 50 times

0

People what the difference of the following codes in php?

 if ($resultado_barganha->tipo === "t") { }

And:

if ($resultado_barganha->tipo == "t") { }

From what I’ve noticed they do the same thing. However netbeans recommends === because?

  • 1

    Looks like duplicate http://answall.com/q/73021/91

3 answers

2


== checks whether the conditions under which the comparison was made are equal, no matter the type, for example

if(true == true) // Verdadeiro
if(true == 1) // Verdadeiro

=== it checks whether the conditions are equal and also checks the type, that is, both the information and the type have to be equal, for example

if(true === true) // Verdadeiro
if(true === 1) //Falso
if(1 == 1) //Verdadeiro

Returns false because 1 is integer and true is not of type integer but boolean

1

When using == it is only checked whether the two values are equal.

But when you use === it is checked if the two values are equal and if there is no information it will be set as "t", never used with a string, usually use to do for example:

if ($resultado_bargain->type === false) { }

1

According to the document:

$a == $b    Igual   Verdadeiro (TRUE) se $a é igual a $b.

$a === $b   Idêntico    Verdadeiro (TRUE) se $a é igual a $b, e eles são do mesmo tipo.

There’s no better explanation than that, at least I think.

Reference

Browser other questions tagged

You are not signed in. Login or sign up in order to post.