SHARE:

Author: Posted on February 26, 2013 at 7:28 pm
PHP
TUTORIALS
4

PHP Ternary Operator / PHP Inline IF practical examples

PHP Ternary Operator / PHP Inline IF

The PHP ternary operator or PHP inline if is a shorthand method for doing evaluations. It is useful in many situations, this tutorial provides a few practical examples. It is important to use PHP ternary operators in the right places as although they can cut down on code, they can make it harder to read.

SIMPLE PHP INLINE EVALUATION

Lets start with a really basic example so you can see what the PHP ternary operator looks like.

The following code is simply checking if the variable $one is greater than $two and assigning the Boolean result to $result and then echoing it.

$one = 1;
$two = 2;
$result = ($one > $two) ? true : false;
echo $result;

The output of the above would be the following:

false

PHP INLINE IF WITH FUNCTION

This example demonstrates how you can use the result of a function later on within the inline evaluation. The isValid function just checks the input against an array of valid file extensions and then if the input is a valid file extension it returns the file extension string, or returns Boolean false.

The $result variable is used within the output of the PHP inline if to output the valid file extension.

function isValid($value) {
    $valid = array(
        "pdf",
        "png",
        "docx",
        "jpg"
    );
    if (in_array($value, $valid)) {
        return true;
    }
    return false;
}

$result = (($result = isValid("pdf")) !== FALSE) ? "The valid file extension is: {$result}" : "The file extension was not valid";

The output of the above would be:

The valid file extension is: pdf

PHP TERNARY TO APPLY STYLE

Personally I find the PHP inline if useful when you need to change the style of HTML elements based on PHP output. The following example loops through an array and outputs its content in a list. We have added a PHP inline if to determine if the index of the current array item is an odd or even number, if it is an odd number, then we set the colour to red, if even to green.

This is a good example of where the inline evaluation can be used as it keeps the code clean and tidy instead of adding extra lines where not needed.

$data = array(
    1 => "One",
    2 => "Two",
    3 => "Three",
    4 => "Four",
    5 => "Five",
    6 => "Six"
);

echo "<ul>";
foreach ($data as $index => $value) {
    echo "<li style='color:".(($ index&1) ? "red" : "green")."'></li>";
}
echo "</ul>";

The output of the above would be as follows:

PHP Inline IF

CONCLUSION

There we have some very simple but effective examples of how the PHP ternary evaluation can be used. I will be adding to this list and hopfully creating a comprehensive list of examples.

If you can think of a really cool example, please drop it in the comment box below and I will gladly add it.

  • /li>
  • http://twitter.com/edwardjbeckett Edward Beckett
    • revillwebdesign

      HN? I assure you it hasn’t been stolen from anywhere :-)

      • http://twitter.com/edwardjbeckett Edward Beckett

        No *I* stole the example from HN … :-)

        • revillwebdesign

          Aha! That’s all good then, my bad, thanks for taking the time to comment :)

  • REVIEWS

  • No categories have been added yet

TABLE OF CONTENTS