News:

Alert!!! HTML Evaluation 01- Quiz

Main Menu

PHP if Operators: 007

Started by Cohort Master, Apr 01, 2024, 06:44 AM

Previous topic - Next topic

Cohort Master

Comparison Operators
If statements usually contain conditions that compare two values.

Example:
Check if $t is equal to 14:
<!DOCTYPE html>
<html>
<body>

<?php
$t 
14;

if (
$t == 14) {
  echo 
"Have a good day!";
}
?>


</body>
</html>


To compare two values, we need to use a comparison operator.

Here are the PHP comparison operators to use in if statements:

Operator   Name   Result   Try it
1. ==   Equal   Returns true if the values are equal   
2. ===   Identical   Returns true if the values and data types are identical   
3. !=   Not equal   Returns true if the values are not equal   
4. <>   Not equal   Returns true if the values are not equal   
5. !==   Not identical   Returns true if the values or data types are not identical   
6. >   Greater than   Returns true if the first value is greater than the second value   
7. <   Less than   Returns true if the first value is less than the second value   
8. >=   Greater than or equal to   Returns true if the first value is greater than, or equal to, the second value   
9. <=   Less than or equal to   Returns true if the first value is less than, or equal to, the second value

Logical Operators
To check more than one condition, we can use logical operators, like the && operator:

Example
Check if $a is greater than $b, AND if $a is less than $c:
<?php
$a 
200;
$b 33;
$c 500;

if (
$a $b && $c $a ) {
  echo 
"Both conditions are true";
}

?>