PHP if Operators
Δημοσιευμένα 2024-05-17 07:45:46
0
2χλμ.
Comparison Operators
If
statements usually contain conditions that compare two values.
ExampleGet your own PHP Server
Check if $t
is equal to 14:
$t = 14;
if ($t == 14) {
echo "Have a good day!";
}
Try it Yourself »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 |
---|---|---|---|
== | Equal | Returns true if the values are equal | Try it » |
=== | Identical | Returns true if the values and data types are identical | Try it » |
!= | Not equal | Returns true if the values are not equal | Try it » |
<> | Not equal | Returns true if the values are not equal | Try it » |
!== | Not identical | Returns true if the values or data types are not identical | Try it » |
> | Greater than | Returns true if the first value is greater than the second value | Try it » |
< | Less than | Returns true if the first value is less than the second value | Try it » |
>= | Greater than or equal to | Returns true if the first value is greater than, or equal to, the second value | Try it » |
<= | Less than or equal to | Returns true if the first value is less than, or equal to, the second value | Try it » |
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
:
$a = 200;
$b = 33;
$c = 500;
if ($a > $b && $a < $c ) {
echo "Both conditions are true";
}
Try it Yourself »Here are the PHP logical operators to use in if
statements:
Operator | Name | Description | Try it |
---|---|---|---|
and | And | True if both conditions are true | Try it » |
&& | And | True if both conditions are true | Try it » |
or | Or | True if either condition is true | Try it » |
|| | Or | True if either condition is true | Try it » |
xor | Xor | True if either condition is true, but not both | Try it » |
! | Not | True if condition is not true | Try it » |
We can compare as many conditions as we like in one if
statement:
Example
Check if $a
is either 2, 3, 4, 5, 6, or 7:
$a = 5;
if ($a == 2 || $a == 3 || $a == 4 || $a == 5 || $a == 6 || $a == 7) {
echo "$a is a number between 2 and 7";
}
Try it Yourself »Αναζήτηση
Κατηγορίες
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- Παιχνίδια
- Gardening
- Health
- Κεντρική Σελίδα
- Literature
- Music
- Networking
- άλλο
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness
Διαβάζω περισσότερα
PHP if Statements
Conditional statements are used to perform different actions based on different conditions....
PHP Strings
A string is a sequence of characters, like "Hello world!".
Strings
Strings in PHP are...
PHP Casting
Sometimes you need to change a variable from one data type into another, and sometimes you want a...
PHP do while Loop
The do...while loop - Loops through a block of code once, and then repeats the loop as...
PHP Operators
PHP Operators
Operators are used to perform operations on variables and values.
PHP divides the...