PHP Logical Operators
Last updated onThe 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, ||, &&, ! ).
To learn more about the PHP data types, read this PHP data types. Thank you for reading.