Comparison and logical operators in PHP
When we state a condition that must be evaluated by a conditional, we know that it must be Boolean, that is, it can only be evaluated as true or false. We cannot put as a condition What time is it?, But if we can put as a condition it's five o'clock.
Comparison operators in PHP
When we state a condition that must be evaluated by a conditional, we know that it must be Boolean, that is, it can only be evaluated as "true" or "false". We cannot put as a condition "What time is it? ", But if we can put as a condition" it's five "(in PHP it would be something like: $ hour == 5).
A condition is an affirmation. And that statement, generally, takes the form of a comparison. Compare a variable, a known data, against an expected "probable" value, which is possibly the value that variable has taken. In this way, the condition is no more than a comparison between the real value that was given to a variable, and one of the "assumed" or "possible" values of that variable imagined by us, the programmers.
But to be able to compare in a truly useful and complete way, we must know the comparison operators that we have in PHP. So far, we have only used the simplest comparison operator, == (equal to), but, in many cases, we will need to state the condition not in terms of equality, but we will need to know if one number is greater than another, if it is less, if one text is different from another. Etc. So, depending on whether this condition is true or not, one or another code block is executed. Let's see, then, the list of possible comparison operators, so that we can use them when elaborating complex conditions:
Let's see some examples of these operators used in real conditions. If we want to compare if a value entered is less than a specific number, we have two ways: using the operator <or using the operator <=:
<?php
if($_POST["age"] <18){
print ("Es menor a 18 años");
}
if($_POST["edad"] <=17){
print ("Es menor a 18 años");
}
?>
If we want to know if something is different from something else, we also have two ways of comparing it: with the operator <> or with! =:
<?php
if($_POST["nombre"] <> "Pepe"){
print ("No es Pepe");
}
if($_POST["nombre"]!="Pepe"){
print ("No es Pepe");
}
?>
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).
Comparison and logical operators in PHP.
Retrieved Nov 04, 2024, from
https://disenowebakus.net/en/php-comparison-operators