Php string comparison why cast to float

Ive meets this code

<?php

$a = md5('240610708');
$b = md5('QNKCDZO');

echo "$a\n";
echo "$b\n";
echo "\n";
var_dump($a);
var_dump($b);
var_dump($a == $b); 

This evaluates 2 lines, which can be a number 0exxxxxx. I understand that if they are used in a numeric context, the string will be accepted as a number, confirmed by http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

If the string does not contain the characters ".", "E" or "E", and the numeric value fits into the restrictions of the integer type type (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases, it will be rated as floating.

. , . 0 (). , ( ), . - 'e' 'E', .

, == , .

+4
1

TL; DR

"" PHP. , , , - , .

, PHP ( ). PHP , compare_function, . . , :

case TYPE_PAIR(IS_STRING, IS_STRING):
    zendi_smart_strcmp(result, op1, op2);
    return SUCCESS;

TYPE_PAIR, , .

zendi_smart_strcmp. " " PHP, . :

if ((ret1=is_numeric_string_ex(Z_STRVAL_P(s1), Z_STRLEN_P(s1), &lval1, &dval1, 0, &oflow1)) && (ret2=is_numeric_string_ex(Z_STRVAL_P(s2), Z_STRLEN_P(s2), &lval2, &dval2, 0, &oflow2))) 
{
    //compare as two numbers, I've omitted this
}
else 
{
    string_cmp: //yes, yes, we're using goto
    Z_LVAL_P(result) = zend_binary_zval_strcmp(s1, s2);
    ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(result)));
}

long double - , , float: , , PHP - - , , ( , "1000" "1e3" ==, "255" "0xFF" - "e" () , )

, :

var_dump("0e8" == "0e6"); //true

md5. , ( , 0 x 10^8 == 0 x 10^6). , . , : === :

var_dump("0e8" === "0e6"); //false

, PHP - ( , ). , .

+5

Source: https://habr.com/ru/post/1541240/


All Articles