Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the PHP comparison operator

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to understand the PHP comparison operator, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

The study of various grammars in the PHP language is the basis for us to learn this language, and we need to accumulate experience in the actual coding to achieve proficiency in the use of PHP language. The PHP comparison operators we introduce today, as their names imply, allow you to compare two values. You can also refer to the PHP type comparison table to see examples of different types comparing with each other.

Table 1. PHP comparison operator

Example name result

$a = = $b equals TRUE if $an equals $b.

$a = = $b congruent TRUE, if $an equals $b, and they are of the same type. (introduced by PHP 4)

$a! = $b is not equal to TRUE if $an is not equal to $b.

$a $b is not equal to TRUE if $an is not equal to $b.

$a! = $b non-congruent TRUE, if $an is not equal to $b, or they are of different types. (PHP 4 only)

$a

< $b 小与 TRUE,如果 $a 严格小于 $b。 $a >

$b is greater than TRUE if $an is strict $b.

$a = $b is greater than or equal to TRUE if $an is greater than or equal to $b.

If the PHP comparison operator compares an integer and a string, the string is converted to an integer. If two numeric strings are compared, they are compared as integers. This rule also applies to switch statements.

< ?php var_dump(0 == "a"); // 0 == 0 ->

True var_dump ("1" = = "01"); / / 1 = = 1-> true switch ("a") {case 0: echo "0"; break; case "a": / / never reached because "a" is already matched with 0 echo "a"; break;}? >

If the types of operands are different, compare them according to the following table (in order).

Table 2. PHP comparison operator compares different types

Operand 1 type Operand 1 type result

Null or string string converts NULL to "" for numeric or lexical comparison

Convert any other type of bool or null to bool,FALSE

< TRUE object object 内置类可以定义自己的比较,不同类不能比较,相同类和数组同样方式比较属性(PHP 4 中),PHP 5 有其自己的说明 string,resource 或 number string,resource 或 number 将字符串和资源转换成数字,按普通数学比较 array array 具有较少成员的数组较小,如果运算数 1 中的键不存在于运算数 2 中则数组无法比较,否则挨个值比较(见下例) array 任何其它类型 array 总是更大 object 任何其它类型 object 总是更大 例子 1. 标准数组比较代码 < ?php // 数组是用标准比较运算符这样比较的 function standard_array_compare($op1, $op2) { if (count($op1) < count($op2)) { return -1; // $op1 < $op2 } elseif (count($op1) >

Count ($op2) {return 1; / / $op1 > $op2} foreach ($op1 as $key = > $val) {if (! array_key_exists ($key, $op2)) {return null; / / uncomparable} elseif ($val)

< $op2[$key]) { return -1; } elseif ($val >

$op2 [$key]) {return 1;}} return 0; / / $op1 = = $op2}? >

Ternary operator of PHP comparison operator

Another conditional operator is the "?:" (or ternary) operator. Example 2. Assign default value

Expression (expr1)? (expr2): (expr3) the value is expr2 when expr1 evaluates to TRUE and expr3 when expr1 evaluates to FALSE.

Note: note that the ternary operator is a statement, so its evaluation is not a variable, but the result of the statement. This is important if you want to return a variable by reference. In a function returned by reference, the statement return $var = = 42? $a: $b; will not work, and future versions of PHP will issue a warning about this.

This is the answer to the question about how to understand the PHP comparison operator. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report