PHP string comparison

 Because PHP is a weakly typed language, sometimes unexpected results will appear when comparing. Today we will summarize some situations of PHP variable comparison.





First off, only values ​​of the same data type can be compared, and PHP is no exception. It's just that PHP converts values ​​of different types to the same data type before comparing them. In this way, the value comparison problem in PHP is converted into a conversion problem between different data types in PHP.

String and String Comparison

PHP string comparison is generally a bitwise comparison, so the following expressions can get the results you want.

var_dump ('' == 'null'); // false  
var_dump ('' == '0'); // false

However, once the content of the string is a legal numerical expression (including scientific notation), even if the comparison is all strings, PHP will perform data type conversion, first convert the string to the corresponding integer or float and then compare.

var_dump('0' == '00'); //true 
var_dump('100' == '1E2'); //true 
var_dump('.12' == '0.12'); //true 
var_dump('12aaa ' == '12'); //false because '12aaa' is not all numbers, so no type conversion is performed, and bitwise comparison is still performed 
var_dump('12aaa' == 12); //true because the right side of the comparison operator is an integer type 12, so first perform type conversion, then compare

What if we   ==  replace   it with the ===result? Let's take a look

var_dump ('0' === '00'); // false 
var_dump ('100' === '1E2'); // false 
var_dump ('. 12' === '0.12'); // false 
var_dump ('12aaa' === '12'); // false 
var_dump ('12aaa' === 12); // false

It can be seen that the full equal sign ===will strictly perform bitwise comparison for PHP string comparisons, and will not convert even strings of numeric expressions.

String and boolean comparison

If the types are different, such as comparing strings and booleans, PHP first converts the strings to booleans and then compares them. Let's first look at a set of Demos

var_dump ('' == true); // false 
var_dump ('0' == true); // false 
var_dump ('1' == true); // true 
var_dump ('00 '== true); // true 
var_dump ('0q' == true); // true 
var_dump ('a0' == true); // true 
var_dump ('null' == true); //threaten

The rules for converting PHP strings to booleans are

false if the string is empty; true if the string is not empty; '0' is false

Value comparison of string and number types

If a string is compared with a value of data type, first the string is converted to a value of type number, and then the comparison is performed. The conversion rules are as follows:

If the string is a legal numeric expression (including scientific notation), it is converted to the corresponding number; if the string begins with a digit, or begins with a decimal point followed by a digit, the leading part of the numeric expression is taken converts it to a number; if the string starts with a non-digit, or if the decimal point is followed by a non-digit, it is converted to 0.

So there will be the following result

var_dump ('adkf' == 0); // true 
var_dump ('0asda' == 0); // true 
var_dump ('1ad' == 0); // false 
var_dump ('adkf' == '0asda'); // false 
var_dump ('adkf' == '0'); // false 
var_dump ('0asda' == '0'); // false 
var_dump ('. 12ads' == 0.12); // true 
var_dump ('E2ada' == 100); // false 
var_dump ('1E2asd' == 100); // true 
var_dump ('1E2asd' == '100'); // false

about null

Undefined variables and properties in PHP are equal to Null, so there will be the following results

$a = null;$b;var_dump(isset($a)); //false 
var_dump(isset($b)); //false 
var_dump(isset($c)); //false 
class A{ 
    public $v1 = null; 
    public $v2; 
    } 
    $o = new A(); 
    var_dump(isset($o->v1)); //false 
    var_dump(isset($o->v2)); //false 
    var_dump(isset( $o->v3)); //false 
    var_dump($c == null); //There will be Notice error, but the result is true 
    var_dump($c === null); //There will be Notice error, but the result is true 
    var_dump(is_null($c)); //There will be Notice error, but the result is true

other

There are also some following examples, which also involve type conversion

var_dump ([] == false); // true 
var_dump ('' == false); // true 
var_dump (null == false); // true 
var_dump ('' == null); // true 
var_dump ([] == null); // true 
var_dump ('' == []); // false

Summarize

Due to the weak type feature of PHP, unexpected results sometimes occur when comparing or if judgments are made. So when making judgments in PHP, be sure to keep in mind the types of different variables. In order to prevent unexpected problems, you can use all equals ===or string comparison functions strcmpto judge

Note: The above code all use the 7.3 version to get the result


Post a Comment

Previous Post Next Post