What does the expression '' 2 pigs' + '3 horses' ==' 5 animals' "return true in PHP? [duplicate]

-2

I've already asked a similar question here in StackOverlow at:

However, taking a look at some of the strange things that exist in PHP, I have seen that the following expression returns true .

var_dump('2 porcos' + '3 cavalos' == '5 animais'); // bool(true)

I even agree that in PHP, the expression "2 porcos" + "3 cavalos" will return int(5) , due to the conversion of the initial numeric strings to type int .

But why does the comparison return true , since the content is completely different?

Of course, == compares only the values, but we see clearly that the values are not equal.

I agree that the example below is correct:

var_dump('2 porcos' +  '3 cavalos' == 5); // bool(true)

However, this expression would not have to return false ?

var_dump(5 == '5 animais'); // bool(true)

And even more questionable:

var_dump(5 == '00005 animais'); // bool(true);

Of course, when we do the conversion, "00005 animais" returns 5 , but why is this valid in comparison? This is bad.

I also noticed that in php 5 == '5 cãezinhos' returns true .

And this can be a problem for those who do not know these "curiosities" of language.

See some examples below

5 == '5 cavalos'; // true
'5' == '5 cavalos'; false
'5' == 5; // true
'5 ' == 5; // true
' 5' == 5; // true

Questions:

  • Does PHP always convert the values to then compare them?
  • When can I trust the comparison operation ( == ) and when can I only trust the identical operator ( === )?
asked by anonymous 02.09.2015 / 18:29

3 answers

4

PHP always casts values for Booleans by default, so in some statements where you believe you are comparing numbers for example, PHP is comparing Booleans. Two important things are the order of expressions and the comparison operator.

In the example of the question we used only the equals ( == ) it compares only the values and discards the type (int, bool, string etc).

5 == '5 cavalos'; comparação entre ints
'5' == '5 cavalos'; false pq é uma comparação entre strings
'5' == 5; // true, comparação entre ints 
'5 ' == 5; // true, mesma coisa

== should be used when only the value imports from the type. === should be used when the value and type of the variable imported.

<?php
//O PHP entende isso como comparação entre booleanos, logo imprime 1(true)
//    1     ==   1
if((bool)-1 == 100){
    echo true;
}else{
    echo false;
}

//Comparação entre um bool e int, retorna 0(false) pois os tipos não os mesmos
//    1      ===  1      
if((bool)100 === 100){
    echo true;
}else{
    echo (int)false;
}
//Comparação entre o mesmo valor e tipo, logo é true
// 100 === 100
if(100 === 100){
    echo true;
}else{
    echo (int)false;
}

Output: 101

Note: I have used echo (int)false; to print zero in the examples, without the cast it does not print anything.

    
02.09.2015 / 18:55
3
var_dump(5 == '5 animais'); // bool(true)
E mais questionável ainda:

var_dump(5 == '00005 animais'); // bool(true);

Why would this return false? This has to do with the PHP mechanisms it "looks for" the values that allow you to do this calculation. And this is why PHP is a bad language because it allows most errors just presented warnings. It's a sloppy language ... Like if ($ x = 5) becomes true because x assumes the value of 5. Often small promenores like these become bugs, PHP is sloppy and allows a lot of things not should.

If tomorrow you start programming in C ++ you will see what happens.

Returning to the subject, given that 00005 is equivalent to 5 anywhere in the world, it is more than justifiable that this validation is true.

0 the left does not count for anything, the right is already another conversation:)

If it were: var_dump (5 == '50000 animals') would have to give false.

I hope I have clarified something. And I remember, as far as PHP is concerned, we have to be very careful because it's so easy to slip away and we lose our good ways of programming.

A big hug.

EDIT:

Regarding the == and the ===.

== means the same.

=== means strictly the same. That is, there is a minimum of difference and validation will be false.

I use === when I get parameters in my classes.

Example:

public function teste($table=false)
{
     if($table === false) return false;
}
    
02.09.2015 / 18:40
2
var_dump('2 porcos' + '3 cavalos' == '5 animais'); // bool(true)

When we add a number with a string in PHP, it tries to convert the string to number and then adds the 2, as Concatenation and Mathematical Operations .

And according to Comparison Operators ,

  

If you compare an integer with a string, the string is converted to   a number.

So when running '2 porcos' + '3 cavalos' == '5 animais' the result is 2+3 == 5 co_de 5==5

    
02.09.2015 / 18:33