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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.