What’s a loose comparison?

Asked

Viewed 463 times

29

In the documentation of PHP, on the switch says:

Note: Note that the switch/case does loose comparison.

What is a loose comparison? And what is the difference between a loose comparison and a rigid one?

  • 10

    What a salient question ( ° ʖ °) (joke, +1)

  • When you compare the value and not the data type. This is what I understood from the above comments

  • 1

    Gee, I thought that was the word "sucker" kkk

2 answers

25


The loose comparison is the default for PHP. As a weak typing language, it leaves aside the rigidity of types at the time of buying, so one tries to get a result, even if one is comparing bananas with apples, which is usually harmful.

In general this is considered bad practice and should only be used if it has a very large benefit, as shown by link above.

The rigid, or strict comparison as it is also called, is made with ===, taking into account the type of data, so if the type is different, it is already guaranteed that the result is false. Loose comparison uses the ==. The documentation makes it clear that at the time of the switch is this comparison that will be used. Then this will go into a case

$x = "1";
switch ($x) {
    case 1:
        echo "é 1";
        break;
    case 2:
        echo "é 2";
        break;
}

See on ideone the probably unwanted effect of executing something that should be different. And in the repl it.. Also put on the Github for future reference.

In strong typing languages none would perform. In static typing languages nor compile.

Note that the case is a numeric type, but the value is a type string. When he tries to compare different types he gets lost.

To prevent this kind of problem from occurring, unlike the if or another construction where you explicitly use the ===, You need to sanitize the data before you compare it, unless you have confidence that it will always be right. But when you do this in practice you’re not using one switch, just use its syntax. The concept of switch is to make a deviation based on a data table, not to use conditions. Conceptually, conditions will be used, the if is more suitable, mainly used rigid comparison:

$x = "1";
if ($x === 1) {
    echo "é 1";
} else if ($x === 2) {
    echo "é 2";
}

See on ideone as now the result is more intuitive not running any of the blocks since the types are different. And in the repl it.. Also put on the Github for future reference.

Given this difficulty the switch PHP is of little use except for cases where a great discipline is used in the use of variables. Or if you use switch as if it were a if, which again has no advantage. I don’t particularly use. In general this mechanism was created in other languages for performance that was better than the if for these cases, but in PHP there will rarely be any gain and the syntax ends up being verbose and susceptible to errors.

Has more details on the choice of if and switch. And about the new match.

In the documentation there are tables of how comparisons are made. There is much more possibility of getting a true one with loose comparison, and it is not always the desired.

18

The comparison loose does not compare the type as in the comparison rigid.

$x = 1
$y = "1"

$x is different from $y because although they have the same value, the first is a variable to type int and the second is like string.

Loose comparison ignores the type and compares only the value. When you identify a number in the string, it is treated as a number. So 1 equals "1".

Simple example of everyday life, for those who floated around:

$x = 1;
if ($x == true) {

}

The above example returns true because true is equal to 1. But it shouldn’t. That’s why it’s called Loose comparison (loose comparison).

When you need greater consistency in comparisons, use Strict comparison (comparison):

$x = 1;
if ($x === true) {

} else {
    // vai entrar aqui, pois é falso.
}

Note that there is a = the most.

Rigid comparisons with switch case

By default the switch case does not make rigid comparison but it is possible to bypass. Example:

$x = 1;
switch (true) {
    case ($x === "1"):

        break;
}

Comparison table

inserir a descrição da imagem aqui

PHP documentation

Case study

An example of how important it is to know when to use, see this comparison somewhat "dangerous" with md5():

var_dump(md5('240610708') == md5('QNKCDZO'));

0e462097431906509019562988736854 is different from 0e830400451993494058024219903391, nay?

See the difference:

var_dump('0e462097431906509019562988736854' == '0e830400451993494058024219903391');

var_dump('0e462097431906509019562988736854' === '0e830400451993494058024219903391');

Browser other questions tagged

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