Sling Academy
Home/PHP/Comparison Operators in PHP: A Complete Guide

Comparison Operators in PHP: A Complete Guide

Last updated: January 10, 2024

Overview

Comparison operators are fundamental in PHP, enabling developers to make decisions by comparing variables. This guide discusses the intricacies of these operators with practical examples.

Equality and Identity

In PHP, == checks if two values are equal, while === checks if they are identical in value and type. For example:

$x = 5;
$y = '5';
var_dump($x == $y);  // bool(true)
var_dump($x === $y); // bool(false)

This shows that while $x and $y are equal in value, they are not identical since one is a string and the other is an integer.

Inequality and Non-identity

Similar to equality, != or <> checks for inequality, and !== checks for non-identity. Here’s how they work:

$x = 10;
$y = '10';
var_dump($x != $y);  // bool(false)
var_dump($x !== $y); // bool(true)

Greater Than and Less Than

Operators > and < are straightforward; they compare numbers:

$x = 5;
$y = 10;
var_dump($x < $y);   // bool(true)
var_dump($x > $y);   // bool(false)

Combined Operators

PHP 7 introduced the spaceship operator <=>, which returns 0 if values on either side are equal, a negative number if the left is less, or a positive number if the left is greater. Here’s how it’s used:

$x = 5;
$y = 5;
var_dump($x <=> $y);  // int(0)
$x = 2;
$y = 5;
var_dump($x <=> $y);  // int(-3)
$x = 10;
$y = 5;
var_dump($x <=> $y);  // int(1)

Truth-Value Testing

PHP’s loose typing means == can yield surprising results:

$x = 0;
$y = 'hello';
var_dump($x == $y);  // bool(false) 

Coercive and Strict Typing with Declarations

As of PHP 7, you can enforce strict type declarations to avoid unexpected casting:

declare(strict_types=1);

function add(int $a, int $b): int {
 return $a + $b;
}

var_dump(add(2, '3'));  // TypeError

Real World Example: User Authentication

Comparison operators become really powerful in contexts like user authentication. Here’s a basic example:

function authenticateUser($username, $password) {
  // User retrieval logic...
  if ($retrievedPassword === $password) {
    return true;
  }
  return false;
}

Conclusion

In conclusion, PHP’s variety of comparison operators, from basic to complex, offers powerful tools for accurate value assessments within your applications. Mastering their correct usage is crucial for writing robust PHP code.

Next Article: PHP: 3 Ways to Get City/Country from IP Address

Previous Article: Arrow Functions in PHP: A Complete Guide

Series: Basic PHP Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array