Search code examples
phpdatabaseternary

Why is this ternary operator not working?


I would think this variable would be pretty straight forward but it's not working? It does echo the row as need if it's set but if it's "false" or not set it does not print "N/A"? Am i doing something wrong here?

$term = isset($row['term']) ? $row['term'] : 'N/A';

Solution

  • My suggestion would be to try the following:

    $term = !empty($row['term']) ? $row['term'] : 'N/A';
    

    Potentially you have a scenario where $row['term'] is set but there is no value so it still falls into the true condition condition.

    For example the following:

    $row = ['term' => ''];
    
    $term = isset($row['term']) ? $row['term'] : 'N/A';
    
    echo $term; // Prints ''