PHP string comparison using '=='

I did some string tests using '=='. I know that comparing the string '==' is wrong, but there is a strange behavior that I want to solve.

I follow the PHP documentation on this page: http://www.php.net/manual/en/language.operators.comparison.php . This is the test I did.

<?php 
   var_dump( "100" == "1e2" ); //outputs boolean true
   var_dump( (int) "100" ); //int 100
   var_dump( (int) "1e2" ); //int 1
?> 

The documentation says that when we compare strings with numbers, PHP first converts the string to numbers, but when I convert '100' and '1e2' to numbers, they are not equal. The comparison should output a boolean false.

Why is this weird behavior? Thank!

+5
source share
2 answers

. 1e2 - float ( , ). float, int s:

<?php 
   var_dump( "100" == "1e2" ); // bool(true)
   var_dump( (float) "100" );  // float(100)
   var_dump( (float) "1e2" );  // float(100)
?> 
+6

Juggling Casting

" "

- float, float, float. , .

+1

All Articles