How to Install Operating System ??Complete Guide Installing The Operating System

Comparison Operators In PHP

e-bookcomputer.com – In this tutorial, we will discuss the Comparison Operators In PHP, what are Comparison Operators? This type of operator is used to compare or match two or more operands, the point is as a determinant whether or not it is suitable, usually used in “if else” conditions.

Before going forward, there are a number of articles that you might be looking for.

Read too :

Types Of Operators Comparison In PHP

There are several types of php comparison operators that can be used, basically, in each programming language the operator comparison is not much different so if you can use comparison operators in PHP it will be able to use it in other languages, here is a list type of Operators Comparison PHP

Comparison Operators In PHP

OperatorNameExampleInformation
!ERROR! unexpected operator '='Together with$a == $bIf $a is equal to $b then TRUE
!ERROR! unexpected operator '='Identical with$a === $bIf $a is equal to (data type must be the same) $b then TRUE
!=Not equal to$a != $bIf $a is not equal to $b then TRUE
!==Not identical with$a !== $bIf $a is not identical to $b then TRUE
<Less than$a < $bIf $a is smaller than $b then TRUE
>More than$a > $bIf $a is greater than $b then TRUE
<=Less than or equal to$a <= $bIf $a is less than/equal to $b then TRUE
>=More than or equal to$a >= $bIf $a is greater than/equal to $b then TRUE

There are several points that need to be considered from the comparison operator above, that some PHP operators require conformity of the same data type “===” to determine a condition where each variable compared has the same type, besides PHP gives access if a variables that are compared to other variables with the same data type are not a problem.

To see the real case in the world of programming, it would be good to always practice, because in general if only theory we will not know what cases will happen someday, the more hours of flying, the more we know.

Comparison Operators In PHP
Comparison Operators In PHP

PRACTICES FOR USING PHP COMPARISON OPERATORS

After we understand the table, let’s practice using comparison operators, here is an example of using comparison operators in php:

1. Same as (==)

<?php
     $a = 10;
     $b = 10;
     if ($a == $b)
     {
        echo 'Right';
     }
     else
     {
        echo 'Salah';
     }
?>

From the example code fragment above, it can be seen in the process condition that occurs that the $a variable has the same value as the $b variable so that it produces the True statement.

2. Identical to (===)

<?php
     $a = 10;
     $b = 10;
     if ($a === $b)
     {
         'True' echo;
     }
     else
     {
         'False' echo;
     }
?>

From the example code fragment above in the process conditions that occur that the $a variable is identical with the variable $b so that it produces the True value, then what is the difference with Equal (===), pay attention to the data types $a and $b where each uses type integer data, so that it produces a True value, please replace one of the types of variable data types above with strings, and separate two conditions Same with (==) and Identical with (===) by using the same value and using different data types.

3. Not the same as (! =)

<?php
     $a = 10;
     $b = 10;
     if ($a! = $b)
     {
         'True' echo;
     }
     else
     {
         'False' echo;
     }
?>

From the example, the condition of the comparison above produces a False value, because the variables $a and $b have the same value.

4. Not identical to (! ==)

<?php
     $a = 10;
     $b = 10;
     if ($a! == $b)
     {
         'True' echo;
     }
     else
     {
         'False' echo;
     }
?>

From the example, the operator comparison above produces an incorrect value, because the variables $a and $b have the same data type.

5. Less than (<)

<?php
     $a = 5;
     $b = 10;
     if ($a <$b)
     {
         'True' echo;
     }
     else
     {
         'False' echo;
     }
?>

From the above conditions, it returns true because the value contained in the variable $a is smaller than that found in the variable $b.

6. More than (>)

<?php
     $a = 10;
     $b = 5;
     if ($a> $b)
     {
         'True' echo;
     }
     else
     {
         'False' echo;
     }
?>

From the example, the condition above produces the value True because the value of the variable $a is greater than the value found in the variable $b.

7. Less than or equal to (<=)

<?php
     $a = 5;
     $b = 10;
     if ($a <= $b)
     {
         'True' echo;
     }
     else
     {
         'False' echo;
     }
?>

From the above conditions will produce a True value because the value in the variable $a is smaller than the value in the variable $b, even though the number of variables $a is equal to the variable $b will still display the condition True, except $a more than the variable $b.

8. More than or equal to (> =)

<?php
     $a = 10;
     $b = 5;
     if ($a> = $b)
     {
         'True' echo;
     }
     else
     {
         'False' echo;
     }
?>

The same thing happens in point number 7, only the difference is that this operator states the value of the opposite condition.

Thus the comparison of operators of this tutorial in PHP, hopefully, it can be practiced directly without just going through theory. thanks.

Before proceeding, there are a number of articles that might be looking for.

Also read :

Sending
User Review
0 (0 votes)

Add a Comment

Your email address will not be published. Required fields are marked *