


Detailed interpretation of PHP comparison operators_PHP tutorial
Table 1. PHP comparison operators
Example name result
$a == $b equals TRUE if $a equals $b.
$a === $b is congruent TRUE if $a is equal to $b and they are of the same type. (Introduced in PHP 4)
$a != $b not equal TRUE if $a is not equal to $b.
$a <> $b not equal TRUE if $a is not equal to $b.
$a !== $b Not Congruent TRUE if $a is not equal to $b, or their types are different. (PHP 4 only)
$a < $b is less than TRUE if $a is strictly less than $b.
$a > $b is greater than TRUE if $a is strictly $b.
$a <= $b is less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b is greater than or equal to TRUE if $a is greater than or equal to $b.
If the PHP comparison operator compares an integer and a string, the string will be converted to an integer. If comparing two numeric strings, compare as integers. This rule also applies to switch statements.
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</SPAN><SPAN> ?php </SPAN></SPAN><LI><SPAN>var_dump(</SPAN><SPAN class=attribute>0</SPAN><SPAN> == "a"); // </SPAN><SPAN class=attribute>0</SPAN><SPAN> == 0 -</SPAN><SPAN class=tag>></span><span> true </span><li class="alt"> <span>var_dump("1" == "01"); // </span><span class="attribute">1</span><span> == 1 -</span><span class="tag">></span><span> true </span> </li> <li><span>switch ("a") { </span></li> <li class="alt"><span>case 0: </span></li> <li><span>echo "0"; </span></li> <li class="alt"><span>break; </span></li> <li><span>case "a": // never reached because "a" is already matched with 0 </span></li> <li class="alt"><span>echo "a"; </span></li> <li><span>break; </span></li> <li class="alt"><span>} </span></li> <li> <span class="tag">?></span><span> </span> </li></span></li></ol>
If the operands are of different types, they are compared (in order) according to the following table.
Table 2. PHP comparison operators compare different types
Operator 1 Type Operand 1 Type result
null or string string Convert NULL to "" , perform numerical or lexical comparison
bool or null Any other type is converted to bool, FALSE < TRUE
object object Built-in classes can define their own comparisons, different classes cannot be compared, and attributes of the same class and arrays are compared in the same way ( PHP 4), PHP 5 has its own instructions
string, resource or number string, resource or number Convert strings and resources to numbers, compared by normal math
array array has fewer members The array is smaller. If the key in operand 1 does not exist in operand 2, the array cannot be compared, otherwise the values are compared one by one (see the example below)
array Any other type of array is always larger
object Any other type of object is always larger
Example 1. Standard array comparison code
<OL class=dp-xml><LI class=alt><SPAN><SPAN class=tag><</SPAN><SPAN> ?php </SPAN></SPAN><LI><SPAN>// 数组是用标准比较运算符这样比较的 </SPAN><LI class=alt><SPAN>function standard_array_compare($op1, $op2) </SPAN><LI><SPAN>{ </SPAN><LI class=alt><SPAN>if (count($op1) </SPAN><SPAN class=tag><</SPAN><SPAN> </SPAN><SPAN class=tag-name>count</SPAN><SPAN>($op2)) { </SPAN><LI><SPAN>return -1; // $op1 </SPAN><SPAN class=tag><</SPAN><SPAN> $op2 </SPAN><LI class=alt><SPAN>} elseif (count($op1) </SPAN><SPAN class=tag>><span> count($op2)) { </span></p> <li> <span>return 1; // $op1 </span><span class="tag">></span><span> $op2 </span> </li> <li class="alt"><span>} </span></li> <li> <span>foreach ($op1 as $</span><span class="attribute">key</span><span> =</span><span class="tag">></span><span> $val) { </span> </li> <li class="alt"><span>if (!array_key_exists($key, $op2)) { </span></li> <li><span>return null; // uncomparable </span></li> <li class="alt"> <span>} elseif ($val </span><span class="tag"><</SPAN><SPAN> $op2[$key]) { </SPAN><LI><SPAN>return -1; </SPAN><LI class=alt><SPAN>} elseif ($val </SPAN><SPAN class=tag>></span><span> $op2[$key]) { </span> </li> <li><span>return 1; </span></li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"> <span>return 0; // $</span><span class="attribute">op1</span><span> == $op2 </span> </li> <li><span>} </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> <p><strong>PHP comparison operator ternary operator </strong></p> <p>Another conditional operator is the "?:" (or ternary) operator. Example 2. Assign default value </p> <pre class="brush:php;toolbar:false"><ol class="dp-xml"><li class="alt"><span><span class="tag"><</SPAN><SPAN> ?php </SPAN></SPAN><LI><SPAN>// Example usage for: Ternary Operator </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>action</SPAN><SPAN> = (empty($_POST['action'])) <br>? 'default' : $_POST['action']; </SPAN><LI><SPAN>// The above is identical to <br>this if/else statement </SPAN><LI class=alt><SPAN>if (empty($_POST['action'])) { </SPAN><LI><SPAN>$</SPAN><SPAN class=attribute>action</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>'default'</SPAN><SPAN>; </SPAN><LI class=alt><SPAN>} else { </SPAN><LI><SPAN>$</SPAN><SPAN class=attribute>action</SPAN><SPAN> = $_POST['action']; </SPAN><LI class=alt><SPAN>} </SPAN><LI><SPAN class=tag>?></span><span> </span></span></li></ol>
Expression (expr1) ? (expr2) : (expr3) When expr1 evaluates to TRUE, the value is expr2, and when expr1 evaluates to FALSE, the value is expr3.
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. The statement return $var == 42 ? $a : $b; in a function that returns by reference will not work, and a future version of PHP will issue a warning about this.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
