EDUCBA

EDUCBA

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

Logical Operators in PHP

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

Logical Operators In PHP

Introduction to Logical Operators in PHP

PHP operators are symbols which help in doing the logical operations with ease. The code generated with these operators helps in performing some specific actions. The logical operators include operators like addition (+), greater than (>), etc. which instruct the compiler to perform the necessary operation. It can check multiple operations and then determine which of the conditions are true. The values which are particular operators use are known as operands. The operators are not similar to functions, though there can be cases where they can be used as functions.

Logical Operators in PHP

Let us now look into Logical Operators in detail. We have six kinds of logical operators. They are as below:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

6 logical operators

PHP also has logical operators that help in combining conditional statements. To name a few of these they are AND, OR, NOT, etc.

1. AND (AND)

The AND operator returns true if both variables being compared are true.

<?php
$x = 100;
$y = 50;
if ($x == 100 and $y == 50) {
echo "AND is true";
}
?> 

Output:

logical operators in php 1

As an example, we can take an analogy of taps and water. When both taps are not running water will not be flowing down the tap. This means that if both conditions are not satisfied or are False then the results will be False or 0. In a similar way if only one tap is closed and the line of water being the same it is not necessary that water will flow as the pipe is closed.

That means even if one condition is true the result will be False or 0. The last case is when both taps of water are running and the pipe being the same for both taps water will flow through the tap and thus the condition will be true.

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)

2. OR (OR)

In a similar way, the OR operator works if either of the conditions is true.

<?php
$x = 100;
$y = 50;
if ($x == 100 or $y == 80) {
echo "XOR is TRUE";
}
?> 

Output:

logical operators in php

The OR operation can be explained in a way where there are three sinks. Each sink having two taps. Not that the sinks are different for all the pairs the scenarios will be as described ahead. The first scenario will have none of the taps open which means there is no water running. That explains the condition of False or 0. The second case is where one of the taps are open. That means there is water flowing from one tap. This scenario helps you understand that if any of the two conditions are true then the result is true. The third scenario will be when both taps are open. The water will be flowing through both the taps. This explains the condition when both conditions are true then it will return true.

3. XOR

The XOR condition returns true when either or the variables are true, not both are true.

<?php
$x = 100;
$y = 50;
if ($x == 100 xor $y == 80) {
echo "XOR here!";
}
?

Output:

xor

4. NOT

The NOT operator is used when it is required to check if a particular variable is not true. Which means we can use NOT when we have to check if any condition is not true.

<?php
$x = 100;
if ($x !== 90) {
echo "NOT is here";
}
?>

Output:

not operator

In this example, you can see that we are checking if the variable is not 90. The variable x is 100 and which is clearly satisfying the NOT condition. Due to this, we have the output which follows the condition specified and hence you see the output as ‘NOT is here.’

5. AND &&

This is similar to the AND that we saw previously. It will return value as true only when both conditions are true, or when both variables evaluate to be true.

<?php
$x = 100;
$y = 50;
if ($x == 100 && $y == 50) {
echo "&& is true!";
}
?> 

Output:

and

6. OR ||

Similar lines the OR condition is also the same as the OR mentioned above. This operator works even when one of the specified conditions is true. It has similar results just like the tap example mentioned previously. OR having three different sinks can fill the sink even when only one tap is open.

<?php
$x = 100;
$y = 50;
}
if ($x == 100 || $y == 80) {
echo "OR is true!";
}
?> 

Output:

or operator 2

In the above example, the variable x satisfies the condition specified for $x=100 and hence the result displays the message for when the result is true. Though here the condition for variable y is not being satisfied the output is displayed. This is because of the OR condition which works even if one condition is being satisfied.

Conclusion

PHP has many logical operators which make it easy to use. The PHP compiler helps in compiling these operators fast. The logical operators help in performing logical operations. These can be arithmetic operations, logical operations, string operations or array operations. PHP has a facility for performing all these operations. It helps in checking multiple conditions at one time. This saves time and increases the optimization of the PHP compiler. Hence it is advised to use these operators when working with PHP. Logical operators hence work in getting logical operations done easily. These conditions hence help you to get Boolean results and work upon them accordingly.

Recommended Articles

This is a guide to Logical Operators in PHP. Here we discuss the basic concept and different types of logical operators in PHP. You may also have a look at the following articles to learn more –

  1. PHP Operators
  2. PHP Array Search
  3. PHP substr_replace()
  4. PHP Encryption

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