Ternary Operator

Category
php
Date Created
30th Jan 2011
Last Updated
30th Jan 2011

Ternary Operator

Question

What is a ternary operator?

Answer

$variable = condition ? if true : if false

An example is shown below:

$style = isset($_GET['style']) ? $_GET['style'] : 'default';

This checks whether variable $_GET['style'] is set.  If it is variable $style is given this value.  Alternatively, variable $style is given the value 'default'.

Part 1 Part 2 Part 3
$variable = condition if true if false
condition we're testing ? what happens if condition is true what happens if condition is false

Related terms