Comparison Operators in PHP

WBOY
Release: 2024-08-29 12:38:51
Original
919 people have browsed it

The Word Comparison in Comparison Operators in PHP itself says that the operators are generally used for comparing any two values/variable values (variable values can be a string or a number or any other to compare). Equal, Identical, Not Equal, Not identical, Greater than, Less than, Greater than or equal to, Less than or equal to are some of the comparison operator names to compare any two types of similar type of values based on our requirement.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Types of Comparison Operators in PHP

There are different types of comparison operators in PHP programming language too just like the other programming languages. Check out about each comparison operators below with the illustrated examples.

1. Equal Comparison Operator ( == )

Equal Operator result will be TRUE only if the 1st variable value is equal to the 2nd variable value. If the 1st variable’s value is not equal to the 2nd variable’s value then FALSE will be the result/output of the comparison.

Example

This is the program to compare two values (strings or numbers) which are assigned to the variables as values. If those values are the same TRUE will be the output else FALSE will be the output. Based on that output the remaining code will run.

Code:

<?php
//1. comparing only numerical values/numbers using two variables
$pavan = 2;
$kumar = 5;
if($pavan==$kumar){
echo "TRUE : Because the two variable's values are same \n";
}
else{
echo "FALSE : Because the two variable's values are not same \n";
}
//2. Program to compare two string values
$a = "pavan";
$b = "pavan";
if($a==$b){
echo "TRUE : String values assigned to the two variables are same \n";
}
else{
echo "FALSE : String values assigned to the two variables are not same \n";
}
?>
Copy after login

Output:

Comparison Operators in PHP

2. Identical Comparison Operator (===)

This identical operator will give results as TRUE if the two variables values belong to the same data type variables else the result will be FALSE.

Example

The below program will be bool(false) because the two values which are in x1, y1 variables don’t belong to same data type so the result will be false.

Code:

<?php
$x1 = 100;
$y1 = "100";
var_dump($x1 === $y1); // will give result as false because types are not at all equal
?>
Copy after login

Output:

Comparison Operators in PHP

3. Not Equal Comparison Operator (!= or <>)

Not the Equal Operator result will become TRUE if the 1st variable’s value is not the same as the 2nd variable’s value else the result will be FALSE. Check the examples below and let know by yourself.

Example #1

Code:

<?php
$pavan1 = 1;
$sake1 = 2;
if($pavan1!=$sake1){
echo "TRUE :: variables values are not same as you expected";
}
else{
echo "FALSE :: variables values are same as not you expected";
}
?>
Copy after login

Output:

Comparison Operators in PHP

Example #2

Code:

<?php
$pavan1 = 1;
$sake1 = 2;
if($pavan1<>$sake1){
echo "TRUE :: variables values are not same as you expected .";
}
else{
echo "FALSE :: variables values are same as not you expected";
}
?>
Copy after login

Output:

Comparison Operators in PHP

4. Not Identical Comparison Operator (!==)

Not Identical operators will produce the TRUE result only when the values of the two variables do not belong to the same data types else the Not identical operator will produce the FALSE result if the data types of the variable’s value are the same.

Example

This program below is to illustrate how the not identical comparison operator operates.

Code:

<?php
$x2 = 100;
$y2 = "100";
var_dump($x2 !== $y2); // returns/provide result as true because types are not at all equal
?>
Copy after login

Output:

Comparison Operators in PHP

5. Less than Comparison Operator (<)

Less than the operator is used to check whether the 1st variable value is less than the 2nd variable value or 2nd variable value is less than the 1st variable value.

Example

The below program will provide result/statements which is in the IF condition because the x3 is less than y3 which is in the IF condition.

Code:

<?php
$x3 = 1473;
$y3 = 1474;
if($x3<$y3){
echo "x3 value :: $x3 \n";
echo "y3 value :: $y3 \n";
echo "x3 value is less than y3 value \n";
}
else{
echo "x3 value is less than y3 value";
}
?>
</h4>
<p><strong>Output:</strong></p>
<p><img  src="https://img.php.cn/upload/article/000/000/000/172490633931064.png" alt="Comparison Operators in PHP" ></p>
<h4>6. Greater than Comparison Operator (>)</h4>
<p>Greater than operator is used to check whether the 1<sup>st</sup> variable value is greater than the 2<sup>nd</sup> variables value or 2<sup>nd</sup> variables value is greater than the 1<sup>st</sup> variable’s value. These comparison operators are very helpful when performing some operations in many programs from simple to complex.</p>
<h5>Example</h5>
<p>The below greater than operator’s program is to implement and to check which variables value is greater than the other variable value.</p>
<p><b>Code:</b></p>


<pre class="brush:php;toolbar:false"><?php
$x4 = 2020;
$y4 = 2019;
echo "x4 value :: $x4 \n";
echo "y4 value :: $y4 \n";
if($x4>$y4){
echo "x4 value is greater than y4 value \n";
}
else{
echo "y4 value is less than x4 value";
}
?>
Copy after login

Output:

Comparison Operators in PHP

7. Less than or Equal to Comparison Operator (<=)

Less than or Equal to the operator will helps in checking whether the 1st variable value is less than or equal to the 2nd variable value or not. It will check and prolong its program to proceed further.

Example

Code:

<?php
$x5 = 2020;
$y5 = 2020;
echo "x5 value :: $x5 \n";
echo "y5 value :: $y5 \n";
if($x5<=$y5){
echo "TRUE :: x5 value is less than or equal to y5 value \n";
}
else{
echo "FALSE :: y5 value is less than x5 value";
}
?>
</h4>
<p><strong>Output:</strong></p>
<p><img  src="https://img.php.cn/upload/article/000/000/000/172490634339597.png" alt="Comparison Operators in PHP" ></p>
<h4>8. Greater than or Equal to Comparison Operator (>=)</h4>
<p>Greater than or Equal to operator helps in checking which number/variable’s value is greater than or equal to which number/other variables value. It also requires two variables values.</p>
<h5>Example</h5>
<p>X6 variables value can either be greater than or equal to the y6 variable’s value. Even though x6,y6 variables value are the same it will execute the statements in the IF condition only.</p>
<p><b>Code:</b></p>
<pre class="brush:php;toolbar:false"><?php
$x6 = 2020;
$y6 = 2020;
echo "x6 value :: $x6 \n";
echo "y6 value :: $y6 \n";
if($x6>=$y6){
echo "TRUE :: x6 value is greater than or equal to y6 value \n";
}
else{
echo "FALSE :: y6 value is less than x6 value";
}
?>
Copy after login

Output:

Comparison Operators in PHP

The above is the detailed content of Comparison Operators in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!