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 $_SERVER: Basics and Examples

    PHP $_SERVER is a superglobal. It’s a predefined variable containing information about your server, client, and request environment. As a…

    PHP JSON Handling with json_encode & json_decode

    JSON is used in most of your web applications for dealing with data, and so, in a way, PHP JSON…

    PHP Type Hints: Ensuring Data Integrity

    PHP-type hinting is something that keeps your code in check and helps your functions receive just the right types of…

    PHP Null: How to Assign and Check for Null Values

    PHP introduced null to handle undefined or missing values. It helps prevent errors when you check if a variable exists.…

    Understanding PHP Constants: A Simple Guide with Examples

    In regard to the use of constants in PHP, clear understanding of their definition and use could be critical in…

    PHP Callable & Callback: Pass a Function to Another

    The callback in PHP lets you pass functions as arguments and reuse code. It helps you to control how tasks…

    PHP Array: Accessing and Managing Elements

    PHP arrays are lists or maps that contain multiple values grouped by array keys. They may include integers, strings, booleans,…

    PHP strpos Function: How it Works with Examples

    Sometimes the string you expect is there, but strpos function gives a surprise in PHP. It may show the result…

    PHP substr Function: How to Extract and Manipulate Strings

    The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally…

    PHP function_exists: Avoid Function Redeclaration

    The function_exists() function in PHP checks whether a given function is defined or not. The function_exists() function works for both…

    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.