Php 7 Comparison Operator Video Instruction Code Examples Lesson 15

Php 7 Comparison Operators compare 2 or more values . There are several different ways to perform these operations. A “Php 7 comparison operator” will check to see if the values are equal, identical, greater than or less than each other. Let’s go over the different methods to use this handy tool. Files from last lesson. Files from this lesson.

Equal To (==) Php 7 Comparison Operator

The equal to (==) comparison operator is checking to see if one value is equal to another value. Consider this example.

<?php /*TO CHECK IF A VALUE IS
EQUAL TO ANOTHER VALUE
THIS COULD BE ANY STRING OR INTEGER
WE USE THE (==) COMPARISON OPERATOR*/
$x = 5;
$y=6;
if($x==$y){
echo "<p>$x and $y are equal to each other</p>";
}else{
echo "<p>$x and $y are not equal to each other</p>";
} ?>

 

Only cool people share!

The answer would be they are not equal to each other. However, consider using a string like this code example.

<?php /*STRING COMPARISON*/
$a = '5';
$b=5;
if($a==$b){
echo "<p>$a and $b are equal to each other</p>";
}else{
echo "<p>$a and $b are not equal to each other</p>";
} ?>

Notice that one variable is an integer while the other is a string. The answer is they are still equal to each other. This will not work with the identical Php Comparison Operator.

Identical (===)

If you want to make sure that something is identical use the triple equals sign (===). Consider this example.

<?php /*TO CHECK IF A VALUE IS
IDENTICAL TO ANOTHER VALUE WE USE (===)
COMPARISION OPERATOR
THIS COULD BE ANY STRING OR INTEGER*/
$a = '5';
$b=5;
if($a===$b){
echo "<p>$a and $b are Identical to each other</p>";
}else{
echo "<p>$a and $b are <span class=\"font-weight-bold text-uppercase\">not</span> Identical to each other</p>";
}
//NOTE: SINCE ONE IS A STRING AND THE OTHER IS AN INTEGER THEY ARE NOT IDENTICAL
?>

 

Not Identical (!==)

When looking for something that is not identical you can use the not identical (!==). Here is a code example.

<?php /*TO CHECK IF A VALUE IS
NOT IDENTICAL TO ANOTHER VALUE
THIS COULD BE ANY STRING OR INTEGER
WE USE THE (!==) COMPARISON OPERATOR*/
$x = 3;
$y= '3';
if($x !== $y){
echo "<p>$x and $y are not identical to each other</p>";
}else{
echo "<p>$x and $y are identical to each other</p>";
} ?>

Greater Than (>)

To see if a value is greater than another value we would use the greater than (>) Comparison Operator. Consider this code example.

<?php
/*TO CHECK IF A VALUE IS
GREATER THAN ANOTHER VALUE
THIS COULD BE ANY STRING OR INTEGER
WE USE THE GREATER THAN (>) COMPARISON OPERATOR*/
$x = 5;
$y= 4;
if($x > $y){
echo "<p>$x is greater than $y</p>";
}else{
echo "<p>$x is less than or equal to $y</p>";
}
?>

 

Less Than (<)

To see if a value is less than another value we would use the less than (<) Comparison Operator. See this code example.

<?php /*TO CHECK IF A VALUE IS
LESS THAN ANOTHER VALUE
THIS COULD BE ANY STRING OR INTEGER
WE USE THE LESS THAN (<) COMPARISON OPERATOR*/
$x = 3;
$y= 4;
if($x < $y){
echo "<p>$x is less than $y</p>";
}else{
echo "<p>$x is greater than or equal to $y</p>";
} ?>

Greater Than or Equal To(>=) Php 7 Comparison Operator

To see if a value is greater than or equal to another value then we would use the greater than and equal to (>=) Comparison Operator. Here is a code example.

/*TO CHECK IF A VALUE IS
GREATER THAN OR EQUAL TO ANOTHER VALUE
THIS COULD BE ANY STRING OR INTEGER
WE USE THE GREATER THAN AND EQUALS (>=)
COMPARISION OPERATORS*/
$x = 5;
$y= 4;
if($x >=$y){
echo "<p>$x is greater than or equal to $y</p>";
}else{
echo "<p>$x is less than $y</p>";
}

Less Than or Equal To(<=) Php 7 Comparison Operator

To see if a value is less than or equal to another value then we would use the less than and equal to (<=) Comparison Operator. Check out this code example.

<?php
/*TO CHECK IF A VALUE IS
LESS THAN OR EQUAL TO ANOTHER VALUE
THIS COULD BE ANY STRING OR INTEGER
WE USE THE LESS THAN AND EQUALS (<=)
COMPARISION OPERATORS*/
$x = 5;
$y= 4;
if($x <=$y){
echo "<p>$x is less than or equal to $y</p>";
}else{
echo "<p>$x is greater than $y</p>";
}
?>

Space Ship (<=>) Comparison Operator in Php 7

The space ship operator will do all of the above. It’s called a space ship because it looks like one, (<=>). It will check to see if something is less than, equal to or greater than. Take a look at this example.

<?php
//0 IS EQUAL TO
//-1 IS LESS THAN
//1 IS GREATER THAN
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1

?>

Conclusion On Comparison Operators

Comparison operators really help us get the job done. When we have data coming back from a database comparison operators will help us filter the results. It was necessary in this lesson to show you a basic “If Else” statement. In the next lesson we will be going over this in detail.

Php 7 Comparison Operator Video Instruction Code Examples Lesson 15 was last modified: October 29th, 2021 by Maximus Mccullough
Php 7 Comparison Operator Video Instruction Code Examples Lesson 15

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.