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

    How to Use PHP count() to Get Array Length

    PHP developers may be need to determine the number of elements in an array. The count() function in PHP makes…

    PHP array_fill Function: Fill Array with Same Value

    You can use the array_fill function in PHP when you must fill an array with one value. You may want…

    Inheritance in PHP: Share Code from Class to Class & Examples

    PHP developers copied and pasted code across multiple files before inheritance came, which made updates difficult. They updated functions in…

    Find Documents in MongoDB Using PHP

    One of the important tasks is retrieving documents from a collection. MongoDB help us to make this process, but understanding…

    PHP str_replace Function: How it Works with Examples

    If you find a word that does not fit and want to fix it. You can use the PHP str_replace…

    PHP sizeof: How to Count Elements in Arrays with Examples

    The sizeof function in PHP exists to help developers know how many items an array holds. Understand the sizeof Function…

    PHP array_column: How to Extract Values from Arrays

    The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull…

    PHP array_push Function: How it Works with Examples

    PHP array_push lets you add one or more values to the end of an array. It appeared to make adding…

    PHP Object | How to Create an Instance of a Class

    The PHP object is an instance from the PHP class or the main data structure that is already been created…

    Static Method in PHP: How They Work in Classes

    PHP static method lets you call functions without an object. In this article, we will cover the following topics: The…

    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.