Logical operators in PHP
In other cases, we will need to create complex conditions, which want to combine (join) two or more simple conditions into one, to evaluate them as a single condition. The element that allows you to combine them is called the logical operator.
In other cases, we will need to create complex conditions, which want to combine (join) two or more simple conditions into one, to evaluate them as a single condition. The element that allows you to combine them is called the logical operator.
Let's look at a simple example without going into detail to try to understand the concept. Suppose we have to find out if a user's age is between 18 and 65 years old to show him a special text. Two conditions have to be met: the first is that their age is greater than 17 (or "greater than or equal to" 18), and the second is that their age is less than 66 (or "less than or equal to" 65):
$_POST["age"]> 17
and
$_POST["age"]< 66
To evaluate these two conditions with a single conditional, we need to "unite" them, and convert them into a single condition that is evaluated as true (if it is greater than 17 and, in addition, less than 66) or false, otherwise. That which is used to join the two conditions into one is what we call a logical operator. In this example, the required operator is and (means y, which is the name of the conjunction operator). In order for the complete expression to return true, we need the first condition to be true and the second condition to be true.
In this case, we would ask you to enter the age of a text field (suppose that the name of that field is "age") and evaluate it in the following way (in passing, we observed that we have written each condition with other comparison operators that in the previous case, this time used >= and <= and modifying the value to be compared)
if($_POST["edad"] >=18 and $_POST["edad"] <= 65){
print("It is in the requested range");
} else {
print("Out of range");
}
Note that we combine two complete conditions into one, even each of them could have been placed separately, in a different if, individually:
$_POST["age"]>= 18
and
$_POST["age"]<=65
The only thing we have done is unite them with the operator and. Of course, we can join more than two conditions, using the same or different operator. Imagine that we need to evaluate if the age is greater than 17 and less than 66, or the marital status is "widower"; The three conditions separately would be:
$_POST["age"]>= 18
and
$_POST["age"]<=65
o
$_POST["state"] =="widower"
When joining, we would use this syntax:
if($_POST["age"]>= 18 and $_POST["edad"] <=65 or $_POST["state"] =="widower") {
print("It's in the requested range");
}
In this case, the conditions united by the and (which will be greater than 17 and less than 66) would be evaluated first and if that is true, it is enough for the entire expression to be considered true. In the same way, if that first part was false, but the state was "widowed", being united by the operator or, it returns true, since that operator needs at least one of the conditions to be true, regardless of whether others they are. We can join any number of conditions, varying the operators freely and creating a single condition that will be evaluated as a whole.
ChatGPT Free
Ask questions on any topic
CITE ARTICLE
For homework, research, thesis, books, magazines, blogs or academic articles
APA Format Reference:
Delgado, Hugo. (2019).
Logical operators in PHP.
Retrieved Dec 10, 2024, from
https://disenowebakus.net/en/php-logical-operators