You are currently viewing PHP Relational Operators

PHP Relational Operators

PHP, one of the most popular server-side scripting languages, provides a powerful set of tools for developers to manipulate data and make decisions in their programs. Among these tools, relational operators are essential in comparing values and determining the relationships between them. In this article, we will explore PHP relational operators, their functionalities, and how they contribute to building robust and efficient applications.

What Are Relational Operators?

Relational operators, also known as comparison operators, are used to compare two values and return a boolean result indicating the relationship between them. PHP supports a variety of relational operators that allow developers to create conditional statements and control the flow of their programs. In PHP, there are several relational operators, each serving a specific purpose.

Equal Operator (==)

The equal operator (==) compares two values for equality, disregarding their data types. This means that if the values are equal after type coercion, the operator returns true.

<?php

    $num1 = 10;
    $num2 = "10";

    if ($num1 == $num2) {
        echo "Equal";
        
    } else {
        echo "Not Equal";
        
    }

In this example, despite one variable being an integer and the other a string, the equal operator considers them equal, resulting in the output “Equal.”

Identical Operator (===)

The identical operator (===) compares both values and their data types. It returns true only if both the values and their types are the same.

<?php

    $num1 = 10;
    $num2 = "10";

    if ($num1 === $num2) {
        echo "Identical";
		
    } else {
        echo "Not Identical";
		
    }

Here, the identical operator considers the variables as not identical due to the difference in data types, resulting in the output “Not Identical.”

Not Equal Operator (!=)

The not equal operator (!=) checks if two values are not equal and returns true if they are different, irrespective of their data types.

<?php

    $num1 = 5;
    $num2 = "10";

    if ($num1 != $num2) {
        echo "Not Equal";
		
    } else {
        echo "Equal";
		
    }

In this case, the not equal operator considers the variables as not equal, leading to the output “Not Equal.”

Not Equal Operator (<>)

Similar to the != operator, the <> operator checks if two values are not equal. Both operators can be used interchangeably.

<?php

    $num1 = 5;
    $num2 = "10";

    if ($num1 <> $num2) {
        echo "Not Equal";
		
    } else {
        echo "Equal";
		
    }

This example produces the same output as the previous program but uses a different not-equal operator (<>).

Not Identical Operator (!==)

Similar to the not equal operator, the not identical operator (!==) checks both values and their data types, returning true only if they are not identical.

<?php

    $num1 = 5;
    $num2 = "5";

    if ($num1 !== $num2) {
        echo "Not Identical";
		
    } else {
        echo "Identical";
		
    }

The not identical operator considers the variables as not identical due to the difference in data types, resulting in the output “Not Identical.”

Greater Than Operator (>)

The greater than operator (>) compares two values and returns true if the left operand is greater than the right operand.

<?php

    $num1 = 15;
    $num2 = 10;

    if ($num1 > $num2) {
        echo "Greater Than";
		
    } else {
        echo "Not Greater Than";
		
    }

In this example, the greater than operator determines that $num1 is greater than $num2, resulting in the output “Greater Than.”

Less Than Operator (<)

Conversely, the less than operator (<) returns true if the left operand is less than the right operand.

<?php

    $num1 = 8;
    $num2 = 12;

    if ($num1 < $num2) {
        echo "Less Than";
		
    } else {
        echo "Not Less Than";
		
    }

Here, the less than operator identifies that $num1 is less than $num2, leading to the output “Less Than.”

Greater Than or Equal To Operator (>=)

The greater than or equal to operator (>=) returns true if the left operand is greater than or equal to the right operand.

<?php

    $num1 = 10;
    $num2 = 10;

    if ($num1 >= $num2) {
        echo "Greater Than or Equal To";
		
    } else {
        echo "Not Greater Than or Equal To";
		
    }

In this case, the greater than or equal to operator considers the values as equal and returns “Greater Than or Equal To.”

Less Than or Equal To Operator (<=)

Similarly, the less than or equal to operator (<=) returns true if the left operand is less than or equal to the right operand.

<?php

    $num1 = 7;
    $num2 = 10;

    if ($num1 <= $num2) {
        echo "Less Than or Equal To";
		
    } else {
        echo "Not Less Than or Equal To";
		
    }

The less than or equal to operator in this example identifies that $num1 is less than $num2, resulting in the output “Less Than or Equal To.”

Spaceship Operator (<=>)

Introduced in PHP 7, the spaceship operator, represented by <=>, is a three-way comparison operator. It returns -1 if the left operand is less than the right operand, 0 if they are equal, and 1 if the left operand is greater than the right operand.

<?php

	$num1 = 15;
	$num2 = 10;

	$result = $num1 <=> $num2;

	if ($result == -1) {
		echo '$num1 is less than $num2';
		
	} elseif ($result == 0) {
		echo '$num1 is equal to $num2';
		
	} else {
		echo '$num1 is greater than $num2';
		
	}

In this example, the value of $num1 is greater than the value of $num2, so the spaceship operator returns 1. The corresponding if block is executed, and the output will be: $num1 is greater than $num2.

Handling Null Values

When dealing with variables that may be null, it’s important to consider the implications of relational operators. Using relational operators directly on null may lead to unexpected results. Instead, it’s recommended to use the null coalescing operator (??) or explicit checks for null.

<?php

	$nullableValue = null;

	if ($nullableValue ?? false) {
		echo "Value is not null";
		
	} else {
		echo "Value is null";
		
	}

In this example, the null coalescing operator ensures that the check is performed correctly, considering the possibility of a null value.

Conclusion

Relational operators are fundamental tools in PHP for comparing values and making decisions based on those comparisons. Whether you’re checking for equality, inequality, or making numerical comparisons, a solid understanding of these operators is essential for writing robust and reliable code.

Leave a Reply