EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Comparison Operators in PHP

Home » Software Development » Software Development Tutorials » PHP Tutorial » Comparison Operators in PHP

comparison operators in php

Introduction to Comparison Operators in PHP

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.

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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";
}
?>

Output:

Comparison Operators in PHP-1.1

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.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

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

Output:

Comparison Operators in PHP-1.2

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";
}
?>

Output:

Comparison Operators in PHP-1.3

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";
}
?>

Output:

Comparison Operators in PHP-1.4

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
?>

Output:

Comparison Operators in PHP-1.5

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";
}
?>

Output:

Comparison Operators in PHP-1.6

6. Greater than Comparison Operator (>)

Greater than operator is used to check whether the 1st variable value is greater than the 2nd variables value or 2nd variables value is greater than the 1st variable’s value. These comparison operators are very helpful when performing some operations in many programs from simple to complex.

Example:

The below greater than operator’s program is to implement and to check which variables value is greater than the other variable value.

Code:

<?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";
}
?>

Output:

Output-1.7

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";
}
?>

Output:

Output-1.8

8. Greater than or Equal to Comparison Operator (>=)

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.

Example:

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.

Code:

<?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";
}
?>

Output:

Output-1.9

Recommended Articles

This is a guide to Comparison Operators in PHP. Here we discuss the Introduction and the types of comparison operators along with its different examples and code implementation. You may also have a look at the following articles to learn more –

  1. Basic PHP Commands
  2. Calendar in PHP
  3. PHP include_once
  4. PHP Array Search

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More