-1

I was working through someone else's code trying to understand their plugin so I could combine it with another plugin to do something different. I have no clue why this question mark is here in the middle of this entry. The code appears as such in one line:

$variable = !empty( $_POST['variable_b'] ) ? explode( "\n", trim( $_POST['variable_b'] ) ) : array();

I understand what the empty, explode, and trim functions do here. I assume that is all being amended to the array. The question mark almost seems like a mini conditional statement.

  • 2
    use web search to check 'php ternary operator' – Michael Sep 1 '17 at 23:26
  • Awesome. Thank you. I was looking through all the PHP stuff I could find and couldn't get this. :) – Chris Thomas Sep 1 '17 at 23:49
2

That's called Ternary Operator.

if( ! empty( $_POST['variable_b'] ) {
    $variable = explode( "\n", trim( $_POST['variable_b'] );
} else {
    $variable = array();
}

You can see PHP Shorthand If/Else Using Ternary Operators (?:) for more details.

Not the answer you're looking for? Browse other questions tagged or ask your own question.