When I used PHP for string comparison, I encountered a pit, as shown in the code:
<?php
var_dump('00E73694765433'=='0'); //true
var_dump('0134b40fsbi94u8'=='0'); //false
var_dump('0134b40fsbi94u8'=='134'); //false
echo PHP_EOL;
var_dump(is_numeric('00E73694765433'));//true
var_dump(is_numeric('0134b40fsbi94u8'));//false
?>
The first result is true, and the second one is false. After looking through the official documents, if the first one is forced to be converted to the number 0, then the second one should be forced to be converted to 134, but if it is converted to 134, the third one should be correct.
I printed two strings, and the result was very strange. I couldn’t figure it out after reading the official documents. Please give me some advice!
Following the voice in heart.
http://www.php.net/manual/zh/...
var_dump('0134b40fsbi94u8'=='134'); //false is comparative
Because they are all strings, we compare the first digit first. The comparison result of the first digit is a comparison between 0 and 1, so it is false
字符串與整形比較的話是轉(zhuǎn)換類型,
字符串與字符串比較就是逐個比較
但是他們是從首位開始比較的,
也就是說 首位假如可以比較大小,就不再往下轉(zhuǎn)換,不能比較的繼續(xù)往下轉(zhuǎn)換。能比較然后就不再往下比較
不能比較就往下接著看!
運(yùn)行下下面這幾個例子 你就明白了
var_dump('0134b40fsbi94u8'=='134')
var_dump('0134b40fsbi94u8'==134)
var_dump('134b40fsbi94u8'==134)
var_dump('134b40fsbi94u8'=='134')
In addition, it is not recommended to use comparison operators to directly compare the sizes between strings. If you need to compare, php also provides some character comparison functions
To compare different types of data, it is best to use the congruent === and incongruent !== operators.
Because the comparison operators congruent === and incongruent !== will check the type:
$a === $b 表示:如果$a等于$b,并且它們的類型也相同時,返回true.
$a !== $b 表示:如果$a不等于$b,或者它們的類型不同時,返回true.
if(false !== 0) echo time(); //輸出時間戳
if(false != 0) echo time(); //沒有輸出
In a word, when comparing, try to use === and!== instead of == and!=.
In addition, PHP also provides a series of type checking functions:
is_int/is_float/is_numeric/is_string/is_bool/is_null
is_array/is_object/instanceof/is_resource/is_callable
Some functions also provide type checking parameters, which need to be paid attention to, such as:
in_array('value', $arr, true)
array_search('value', $arr, true)
array_keys($arr, 'value', true)
If the value of the third parameter is true, the function will also check whether the types are the same.
In addition, PHP also supports the comparison operators ==,!=,===,!== to compare whether two arrays or two objects are equal.
// 輸出true,表示存在相同的鍵值對.
var_export( array('a' => 1, 'b' => '2D') == array('b' => 2, 'a' => 1) );
// 輸出false,因?yàn)?==不僅要求鍵值對相同,而且要求元素的【順序】和【類型】也相同.
var_export( array('a' => 1, 'b' => 2) === array('b' => 2, 'a' => 1) );
// 輸出false,因?yàn)閮蓚€數(shù)組的鍵值對是不同的。
var_export( array('a', 'b') == array('b', 'a') );
左邊是:
array (
0 => 'a',
1 => 'b',
)
右邊是:
array (
0 => 'b',
1 => 'a',
)
PHP also supports comparison operators (==,!=,===,!==) to determine whether two objects are equal:
class Foo {}
$foo1 = new Foo();
$foo2 = new Foo();
var_export($foo1 == $foo2); // true
var_export($foo1 === $foo2); // false (因?yàn)橐貌煌?
Your first string exactly conforms to scientific notation.
For languages ??with loose type restrictions, there are often pitfalls like this in the news.
Try to use strictly typed checks
For weakly typed languages ??like php, if you can use ===
, don’t use it==