If statement with multiple conditions in php

You can use if statement with multiple conditions within single if statement or you can also use if statement with elseif and else condition in php.

Answers 3
  • If statement with OR (||) condition in php

    $var = "abc";
    if($var == "abc" || $var == "def") 
    { 
        echo "true"; 
    }
    
    0

    It will return true if the value of $var is abc or def. If the value we assign to $var is not equal to “abc” or “def” then the if statement will not be executed.

  • If statement with AND (&&) condition in php

    if($var == "abc" && $var2 == "def") 
    { 
        echo "true"; 
    }
    
    0

    It will return true if both conditions are true like if the value of $var is abc and the value of $var2 is def. If any of the values of these two variables is not equal to the passed value after the (==) operator then the if statement will not be executed.

  • If statement with elseif and else condition in PHP

    <?php
    $records = ['first', 'second', 'third'];
    
    if (count($records) === 1){
        echo "I have one record! ";
    }
    elseif (count($records) > 1)
        echo "I have multiple records!";
    else{
        echo "I don't have any records!";
    }
    ?>
    
    0
  • Back to code snippet queries related php