php string comparison
typecho
typecho 2017-06-16 09:18:38
0
4
989

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!

typecho
typecho

Following the voice in heart.

reply all(4)
左手右手慢动作

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

字符串与整形比较的话是转换类型,
字符串与字符串比较就是逐个比较
但是他们是从首位开始比较的,
也就是说 首位假如可以比较大小,就不再往下转换,不能比较的继续往下转换。能比较然后就不再往下比较
不能比较就往下接着看!
运行下下面这几个例子 你就明白了
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

学习ing

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,因为===不仅要求键值对相同,而且要求元素的【顺序】和【类型】也相同.
var_export( array('a' => 1, 'b' => 2) === array('b' => 2, 'a' => 1) );

// 输出false,因为两个数组的键值对是不同的。
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 (因为引用不同)
仅有的幸福

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==

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template