We have the following code block:
<?php
var_dump(md5("240610708")=="0e1337");
?>
Which returns bool (true), that is, for PHP the md5 of 240610708 is the same as any string that begins with 0e followed by a number. How does this happen?
We have the following code block:
<?php
var_dump(md5("240610708")=="0e1337");
?>
Which returns bool (true), that is, for PHP the md5 of 240610708 is the same as any string that begins with 0e followed by a number. How does this happen?
When using the operator
==
to compare strings inPHP
you can get unexpected results. This operator converts strings to numbers and then does the comparison.This conversion is done according to these criteria
If the string starts with a valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (it can optionally contain a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.
That is, the result of the operation
md5(...)
begins with0e...
and you compare it with the string0e...
when doing the numeric conversion, both strings become the number0
that when compared are equal and thereforetrue
.To avoid it, use the operator
===
:What happens is that when you compare with
==
and deal with numbers in strings or the comparison is between a number and a string, it tries to convert to number first, and then does the comparison:And that resulting String converted to an int would be
0
.When you convert the other element of the comparison,
0e1337
, is0
also, ergo the result istrue
.If you use
===
the result would befalse
.online example