0

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...

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by R0bb0b
0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.