Logical Operators in PHP

PHPz
Release: 2024-08-29 12:39:06
Original
333 people have browsed it

PHP operators are symbols that help in doing logical operations with ease. The code generated with these operators helps in performing some specific actions. The logical operators include operators like addition (+), greater than (>), etc., which instruct the compiler to perform the necessary operation. It can check multiple operations and determine which conditions are true. The values which are particular operators use are known as operands. The operators are not similar to functions, though there can be cases where they can be used as functions.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Logical Operators in PHP

Let us now look into Logical Operators in detail. We have six kinds of logical operators. They are as below:

Logical Operators in PHP

PHP also has logical operators that help in combining conditional statements. To name a few of these, they are AND, OR, NOT, etc.

1. AND (AND)

The AND operator returns true if both variables being compared are true.

<?php
$x = 100;
$y = 50;
if ($x == 100 and $y == 50) {
echo "AND is true";
}
?>  
Copy after login

Output:

Logical Operators in PHP

As an example, we can take an analogy of taps and water. Water will not flow down the tap when both taps are not running. This means that if both conditions are not satisfied or False, the results will be False or 0. Similarly, if only one tap is closed and the line of water is the same, it is not necessary that water will flow as the pipe is closed.

That means the result will be False or 0 even if one condition is true. The last case is when both taps of water are running, and the pipe is the same for both taps, water will flow through the tap, and thus the condition will be true.

2. OR (OR)

Similarly, the OR operator works if either of the conditions is true.

<?php
$x = 100;
$y = 50;
if ($x == 100 or $y == 80) {
echo "XOR is TRUE";
}
?>  
Copy after login

Output:

Logical Operators in PHP

Three sinks can explain the OR operation. Each sink has two taps. The sinks are not different for all the pairs; the scenarios will be as described ahead. The first scenario will have no taps open, so there is no water running. That explains the condition of False or 0. The second case is where one of the taps is open. That means there is water flowing from one tap. This scenario helps you understand that the result is true if any of the two conditions are true. The third scenario will be when both taps are open. The water will be flowing through both the taps. This explains that when both conditions are true, it will return true.

3. XOR

The XOR condition returns true when either or the variables are true, not both are true.

<?php
$x = 100;
$y = 50;
if ($x == 100 xor $y == 80) {
echo "XOR here!";
}
?
Copy after login

Output:

Logical Operators in PHP

4. NOT

The NOT operator is used when it is required to check if a particular variable is not true. This means we can use NOT when we have to check if any condition is not true.

<?php
$x = 100;
if ($x !== 90) {
echo "NOT is here";
}
?>
Copy after login

Output:

Logical Operators in PHP

In this example, you can see that we are checking if the variable is not 90. The variable x is 100 and which satisfies the NOT condition. Due to this, we have the output following the specified condition; hence, you see the output as ‘NOT is here.’

5. AND &&

This is similar to the AND that we saw previously. It will return the value as true only when both conditions are true or when both variables evaluate to be true.

<?php
$x = 100;
$y = 50;
if ($x == 100 && $y == 50) {
echo "&& is true!";
}
?>  
Copy after login

Output:

Logical Operators in PHP

6. OR ||

Similar lines, the OR condition is also the same as the OR mentioned above. This operator works even when one of the specified conditions is true. It has similar results, just like the tap example mentioned previously. OR having three different sinks can fill the sink even when only one tap is open.

<?php
$x = 100;
$y = 50;
}
if ($x == 100 || $y == 80) {
echo "OR is true!";
}
?>  
Copy after login

Output:

Logical Operators in PHP

In the above example, the variable x satisfies the condition specified for $x=100, and hence the result displays the message for when the result is true. Though the condition for variable y is not satisfied, the output is displayed. This is because of the OR condition, which works even if one condition is being satisfied.

Conclusion

PHP has many logical operators, which make it easy to use. The PHP compiler helps in compiling these operators fast. The logical operators help in performing logical operations. These can be arithmetic, logical, string, or array operations. PHP has a facility for performing all these operations. It helps in checking multiple conditions at one time. This saves time and increases the optimization of the PHP compiler. Therefore, it is advisable to utilize these operators when working with PHP. Logical operators expedite the execution of logical operations, ensuring quick results. These conditions thus help you to get Boolean results and work on them accordingly.

The above is the detailed content of Logical Operators in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!