DEV Community

Baransel
Baransel

Posted on

What is the logical operators in PHP.

We can evaluate and combine multiple comparisons with logical operators. For example, we make a comparison of whether a variable is greater than 0 or less than 100 thanks to logical operators.

Let's show the logical operators in a table:

Operator Name Usage Explanation
! Negative !$a Returns true if the value of $a is negative, or the other way around.
&& And $a && $b If the value of $a and $b is positive, TRUE returns positive, if either is negative, returns FALSE.
|| Or $a || $b Returns positive if the value of $a or the value of $b is positive. It is enough that one or both are positive.

In an example of these, we said at the beginning; Let's compare the value of a variable "if it is greater than 0 and less than 100". The condition we apply for this is as follows: $a > 0 && $a < 100 This means if the variable $a is greater than 0 and less than 100. If the number in $a is between 0 and 100, the result of this condition will be positive.

Follow my blog for more baransel.dev.

Top comments (0)