Hi all

Just a quick question, I have avoided the ternary operator type If statement a lot in my code, because personally I find it easier to read a statement like this:

if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}

Rather than:

$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

My question is: Is there any advantage to using the Ternary operator over a standard IF statement other than obviously saving a few characters...

Hi all

Just a quick question, I have avoided the ternary operator type If statement a lot in my code, because personally I find it easier to read a statement like this:

if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}

Rather than:

$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

My question is: Is there any advantage to using the Ternary operator over a standard IF statement other than obviously saving a few characters...

Not really, just when you start doing it, it becomes habit forming. Its great for auto filling form fields like drop down lists and checkboxes, so in that case yes, it makes it much easier to read.

This question has already been answered. Start a new discussion instead.