PHP : if Operators

In PHP, there are no specific if operators as such; however, there are various operators that are commonly used within if statements to evaluate conditions. These operators allow you to compare values, check for the existence of a variable, or perform other logical operations. Here are some commonly used operators within if statements:

1. Comparison Operators

  •    Used to compare two values and return a boolean result (true or false).
  •    Examples: `==` (equal), `!=` (not equal), `<` (less than), `>` (greater than), `<=` (less than or equal to), `>=` (greater than or equal to), `===` (identical), `!==` (not identical).

  
   $x = 10;
   if ($x == 10) {
       // Code to be executed if $x is equal to 10
   }

   

2. Logical Operators:

 

  •    Used to combine multiple conditions and return a boolean result.
  •    Examples: `&&` (logical AND), `||` (logical OR), `!` (logical NOT).

 
   $x = 10;
   $y = 20;
   if ($x > 0 && $y < 30) {
       // Code to be executed if $x is greater than 0 AND $y is less than 30
   }

   

3. Null Coalescing Operator (`??`):

  •    Returns the value of the first operand if it exists and is not null, otherwise returns the second operand.
  •    Example:


   $name = $_GET['name'] ?? 'Guest';
   

4. Ternary Operator (`? :`):

 

  •    Short form of an if-else statement.
  •    Syntax: condition ? value_if_true : value_if_false.
  •    Example:

 
   $age = 20;
   $status = ($age >= 18) ? "adult" : "minor";

  

These are just a few examples of the operators commonly used within if statements in PHP. By using these operators effectively, you can write concise and readable code to evaluate conditions and control the flow of your PHP scripts.