Table of Content
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.
| Operator | Name | Description |
|---|---|---|
| “AND” — ”&&” | and operator | To discover the true boolean value of two logical values. |
| “OR” — ”||” | or operator | To discover the one correct value as a boolean true of two logical values. |
| “xor” | xor operator | To 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 operator | To 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 variable function is a variable that is used to assign a function name with parentheses (). However, the PHP interpreter…
User input does not always come through as expected. Sometimes values are missing or not set at all. This can…
At times, you may need to check if a row exists to avoid duplication when inserting or to prevent issues…
The PHP array_diff_ukey function compares keys in two or more arrays. It returns the keys from the first array that…
You can use the array_fill function in PHP when you must fill an array with one value. You may want…
The PHP is_readable helps you validate file readability before any operation like read or include files. What Is PHP is_readable? The is_readable() function…
PHP gives us the ability to work with MongoDB, highlighting the importance of CRUD (Create, Read, Update, Delete) operations for…
PHP mail() is a great built-in function for sending an email directly from your script for account confirmations, password resets, or sending notifications.…
Developers use the array_count_values function in PHP to find how many times each value appears in an array. What is…
PHP introduced null to handle undefined or missing values. It helps prevent errors when you check if a variable exists.…