php conditional operator code example

Example 1: php != operator

$a == $b	Equal	TRUE if $a is equal to $b after type juggling.
$a === $b	Identical	TRUE if $a is equal to $b, and they are of the same type.
$a != $b	Not equal	TRUE if $a is not equal to $b after type juggling.
$a <> $b	Not equal	TRUE if $a is not equal to $b after type juggling.
$a !== $b	Not identical	TRUE if $a is not equal to $b, or they are not of the same type.
$a < $b	Less than	TRUE if $a is strictly less than $b.
$a > $b	Greater than	TRUE if $a is strictly greater than $b.
$a <= $b	Less than or equal to	TRUE if $a is less than or equal to $b.
$a >= $b	Greater than or equal to	TRUE if $a is greater than or equal to $b.
$a <=> $b	Spaceship	An integer less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively. Available as of PHP 7.

Example 2: php conditionals

#Conditionals

<?php
    #Conditionals
    /*
    == equals
    === identical
    < less than
    > greater than
    <= less than or equal to
    >= greate than or equal to
    != not equal to
    !== not identrical
    */
    $num = 5;

    #if($num == 5){
     #   echo '5 passed';
    #}elseif($num == 6){
   #     echo '6 passed';
  #  }else{
 #       echo'did not pass';
#    }

    #nesting If Statements
    
    if($num>4){
        if($num < 10){
            echo "$num passed<br>";
        }
    }
    /*
    Logical operators can be used 
    instead of what is above

    and     &&
    or      ||
    xor          means one has to be true but not both
    */
    if($num >4 && $num <10){
        echo "$num passed using Logical operators";
    }
    /*
    Switch tests for value and then find
    a match and do something
    */
    $favColor = 'pink';

    switch($favColor){
    case 'red':
        echo '<br>Your favorite color is red';
        break;
    case 'green':
        echo '<br>OH NO!! Your favorite color is green';
        break;
    case 'blue':
        echo '<br>Kool!! Your favorite color is blue';
        break;
    default:
        echo '<br>Yikes! Your favorite color is something else';
        
    }

?>

Example 3: operators in php

if (condition)
   code to be executed if condition is true;
else
   code to be executed if condition is false;

Example 4: php ternary

#Ternary & Shorthand Syntax

<?php
    $loggedIn = false;
    $array =[1,2,3,4,5,6];
    if($loggedIn){
        echo 'You are logged in';
        echo '<br>';
    }else {
        echo 'You are NOT logged in';
        echo '<br>';
    }
    //the below is the same thing as above
    echo ($loggedIn) ? 'You are logged in' : 'You are NOT logged in';

    $isRegistered = ($loggedIn == true) ? true : false;
    echo '<br>';
    echo $isRegistered;

    //these can be nested too
    $age = 9;
    $score = 15;
    echo'<br>';
    echo 'Your score is: '.($score > 10 ? ($age >10 ? 'Average': '
    Exceptional'): ($age > 10 ? 'Horrible':'Average'));
    echo'<br>';

?>

  //alternative syntax for conditionals and loops
<!-- alternative syntax for conditionals and loops -->
<div>
<?php if($loggedIn) { ?>
    <h1>Welcome User</h1>
<?php }else{ ?>
    <h1>Welcome Guest</h1>
<?php } ?>
</div>
//<!-- a better way that looks better -->
<div>
<?php if($loggedIn):  ?>
    <h1>Welcome User</h1>
<?php else: ?>
    <h1>Welcome Guest</h1>
<?php endif; ?>
    
</div>

//<!-- An Array can be used too -->
<div>
<?php foreach($array as $val):  ?>
    <?php echo $val; ?>
<?php endforeach; ?>
    
</div>
//<!-- Can also use loops this way -->

<div>
<?php for($i = 0; $i < 10; $i++):  ?>
    <li> <?php echo $i; ?></li>
<?php endfor; ?>
    
</div>

Example 5: php ?:

// Example usage for: Ternary Operator
$action = $_POST['action'] ?: 'default';

// The above is identical to this if/else statement
if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}

Tags:

Php Example