PHP Logical Operators | Understanding the 6 Logical Operators

The PHP logical operators are operands that can be used to check two or more expressions together. For examples ( AND, OR, XOR, NOT, ||, &&, ! ). And they would return a true or false as a logic or a boolean value.

    As we mentioned logical or boolean data type, the logical or boolean data type is printed with 1 or no value in the PHP programming language. So if it has a “true” it will print “1” otherwise, it will print “”.

    Anyway, The following table explains the PHP logical operators with their descriptions. Let’s explain each one in-depth.

    OperatorNameDescription
    “AND” — ”&&”and operatorTo discover the true boolean value of two logical values.
    “OR” — ”||”or operatorTo discover the one correct value as a boolean true of two logical values.
    “xor”xor operatorTo merge two boolean results in the condition block to check for. If one part of two both has a true boolean value, so it will return true, otherwise, it will return a false boolean result.
    “!”Not operatorTo demonstrate the opposite of true or false value according to the operand result.

    The following sections are the explanation of each one in-depth with examples.

    Usage of PHP AND Operator as a Logical Operator

    The AND operator would be as the “&&” or “AND” to discover the true boolean value of two logical values.

    Check the following example of the AND operator with the IF condition.

    <?php
      $url = "https://codedtag.com";
      if ( strlen( $url) > 10 && strpos($url , ".com") !== false ) {
        echo "A correct url"; // will print this line
      }
    ?>

    So the AND operator exposes if the two logic values are corrected values, which must be true boolean values to achieve the condition body.

    Also, you can use AND keyword like the below example.

    <?php
      $url = "https://codedtag.com";
      if ( strpos($url , "codedtag") !== false AND strpos($url , ".com") !== false  ) {
        echo "A correct url"; // will print this line
      }
    ?>

    Let’s move to the OR logical operator.

    Usage of PHP OR Operator as a Logical Operator

    The PHP OR operator would be written like this symbol “||” or “OR”. To discover the one correct value as a boolean true of two logical values. So it only searches for the true boolean value of two both. Let’s see an example.

    <?php
       $username = "Ahmed";
       if ( strlen( $username ) == 5 || $username == "Ahmad" ) {
         echo "Valid Username !";
       }
    ?>

    Also, the OR operator would be with the keyword “OR”. Let’s see that.

    <?php
       $username = "Ahmed";
       if ( strlen( $username ) == 5 OR $username == "Ahmad" ) {
         echo "Valid Username !";
       }
    ?>

    Usage of PHP XOR Operator as a Logical Operator

    This operator is used to merge two boolean values in the condition block to check for. If one part of two, both have a true boolean value. So it will return true, otherwise, it will return a false boolean result.

    It would be written as the “xor” between two boolean expressions. Let’s see an example.

    <?php
      $x = 5;
      $y = 6
      $a = 8;
      $z = 10;
      
      $part_1 = ( $x > $y ); // => false
      $part_2 = ( $z < $a ); // => false
      
      var_dump( $part_1 xor $part_2 ); // the output: bool(false)  
    ?>

    The XOR Operator can be used to check for only one true value in two parts, and that can happen when combining two conditions together. Otherwise, it returns a false boolean value.

    PHP NOT Operator

    The PHP OR operator takes this symbol “!”. And that to show the opposite of true or false value according to the operand result.

    Let’s see an example.

    <?php
      $x = !false;
      var_dump( $x ); // bool(true)
    ?>

    For another one.

    <?php
      var_dump( !$x ); // bool(true)
    ?>

    So, in the previous example, the result was true because the $x variable was not defined in the PHP script. Let’s move to wrap up the general information of this tutorial.

    Wrapping Up

    In this article, you understood what the PHP logical operators are. And discussed each one with examples such as ( AND, OR, XOR, NOT, ||, &&, ! ).

    Similar Reads

    PHP array_fill_keys: Create Arrays with Specific Keys

    The PHP array_fill_keys function helps you to build an array where each key has the same value. What is the…

    PHP Arithmetic Operators: Essential Guide

    Perhaps you're new to PHP or sharpening your skills and looking to understand how numbers work within this language under…

    PHP array_merge: Combine Multiple Arrays into One

    You have two or more arrays. You want one. That is the problem array_merge solves in PHP. This function lets…

    Understanding the PHP Operator Precedence

    The PHP operator precedence refers to when doing a calculation for three or more numbers, they are calculating its values…

    PHP mb_substr Function: How it Works with Examples

    You may need to get part of a text when you work with text in PHP. The mb_substr gives you…

    PHP XML Expat Parser: A Complete Guide

    If you've ever tried to pull data from an XML file, you know it can be a bit tricky at…

    PHP OOP Constructor: How It Works in a Class with Examples

    The OOP constructor initializes an object when it is created in PHP. Understand What a Constructor Is in PHP A…

    PHP array_find_key: How it Works with Examples

    The PHP array_find_key function finds a key in an array by a condition. It checks each element and returns the…

    PHP OOP Programming: A Complete Tutorial

    A complex web application needs proper organization and maintenance to keep your code structured. This is where PHP Object-Oriented Programming…

    PHP range Function: Create Arrays with Sequence Values

    The PHP range function helps you make sequences. Without it, you would write loops every time you need numbers or…

    Previous Article

    Understanding the Increment and decrement operators in PHP

    Next Article

    Concatenating Strings in PHP: Tips and Examples

    Write a Comment

    Leave a Comment

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


    Subscribe to Get Updates

    Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
    No spam. Unsubscribe anytime.