search
HomeBackend DevelopmentPHP TutorialDetailed explanation of php comparison operators

Detailed explanation of php comparison operators

Jun 20, 2017 pm 01:51 PM
phpcomparison operatoroperator

The

operator is a symbol that tells the compiler to perform specific arithmetic or logical operations. It usually forms an expression together with the operands. We often see it participating in mathematical operations or logical operations. PHP also contains many operators. This article explains in detail the very important Comparison Operator.

Types of Comparison Operators

As their names imply, allow comparison of two values. The comparison operators are as follows:
1) $a > $b is greater than: if $a is strictly greater than $b, then TRUE is returned
2) $a 3) $a >= $b greater than or equal to: if $a is greater than or equal to $b, then return TRUE
4) $a 5) $a $b is not equal to: If $a is not equal to $b, then return TRUE
6) $a != $b is not equal to: if If $a is not equal to $b, then TRUE is returned (same as above)
7) $a == $b is equal to: If $a is equal to $b, then TRUE is returned
8) $a === $b is equal to : If $a is equal to $b, and their types are also the same, return TRUE
9) $a !== $b is not equal to: If $a is not equal to $b, or their types are different, return TRUE

Among them, we need to focus on distinguishing between "equal to" and "equal to". $a == $b only performs a comparison operation on the values ​​​​of two variables, while "equal to" requires the expression of both sides of the operator. The formula compares values ​​and data types at the same time. Only when the values ​​on both sides are equal, the operation result is "true". Combined with the "inequality" operator, for example, $a = 2; var_dump($a!==2); this expression returns "false" because 2 is equal to 2. In addition, $a = 2; is an integer type and 2 in var_dump($a!==2); is also an integer type, but the operator is not exactly equal to "!==", so the result is false, because 2 is equal to 2 . On the other hand, if this is the case $a = 2; var_dump($a!=='2'); the result of the operation is "true", because 2 is not equal to '2', and the following '2' is a string 's '2' means that not all equals not only compare the variable values, but also compare the data types of the variables.

Comparing results of different types in PHP

If PHP comparison operator compares an integer and a string, the string will be converted to Compare after integers. If you compare two numeric strings, they are compared as integers, and this rule also applies to switch statements.
For example:
var_dump(0 == "a"); // Returns TRUE, "a" is converted to 0
var_dump("1" == "01"); // Returns TRUE, Treat as an integer

Comparison between string or null and string: Convert NULL to "" to compare numbers or strings
Comparison between bool or null: Convert to bool, FALSE Object built-in classes can define their own comparisons. Different classes cannot be compared, and properties of the same class are compared
Comparison between strings, resources or numbers: Convert strings and resources into numbers, and compare them according to ordinary mathematics
array Comparison between: Arrays with fewer members are smaller. If the key in operand 1 does not exist in operand 2, the arrays cannot be compared and need to be compared value by value (see the following code)
array and any other type Comparison: array is always larger
Comparison between object and any other type: object is always larger

Array comparison code:

 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;    
    } else if ($val < $op2[$key]) { 
       return -1; 
     } elseif ($val > $op2[$key]) { 
       return 1; 
      } 
   } 
   return 0;   // $op1 == $op2 
 }

Comparison operation The ternary operation symbol in the symbol:

expression (expr1) ? (expr2) : (expr3) When the value of expression expr1 is TRUE, the value is expr2, The value of expression expr1 when it evaluates to FALSE is expr3.

The above is the detailed content of Detailed explanation of php comparison operators. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

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.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)