Exploring Equality and Identical Operators

Exploring Equality and Identical Operators

Welcome back! After mastering the basics of making decisions with if and else statements, it's time to take a closer look at equality and identical operators in PHP. These operators help you compare values and make more informed decisions within your programs.

What You'll Learn

In this lesson, you'll learn how to use equality (==) and inequality (!=) operators to compare values. Additionally, you'll understand the importance of using identical (===) and not identical (!==) operators to avoid type conversion issues. By the end of this lesson, you'll be able to:

  • Use equality and inequality operators to compare values.
  • Understand the significance of identical and not identical comparisons to prevent unexpected results.
  • Write conditions that depend on both the value and type of variables.

Equality and Inequality Operators

Identical and Not Identical Operators

The identical (===) and not identical (!==) operators take both the value and type into account. This helps prevent type coercion issues and makes your comparisons more precise. Here's some example code:

PHP
<?php
$fuelString = "75";

// Identical comparison to avoid type conversion issues
if ($fuelString === 75) {
    echo "Fuel level is 75, and the type is identical.\n";
} else {
    echo "Fuel level is not identical to 75 (or type mismatch).\n";
}

// Not identical comparison
if ($fuelString !== 75) {
    echo "Fuel string is not identical to the fuel level as an integer.\n";
}
?>

By using the identical (===) and not identical (!==) operators, you ensure that both the value and the type match exactly or differ, respectively. This provides more accurate comparisons, improving the reliability of your conditions.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal