PHP if shorthand

Viewed 43353

I have a string that I want to append it some other string. let's say:

$my_string = 'Hello';

$my_string .= ' there';

this would return 'Hello there'.

I want to make this conditional like this:

$my_string = 'Hello';

$append = 'do';

if ( $append == 'do' ) {

    $my_string .= ' there';

}

Now, I want to use a ternary operation to do this, but all the examples I came across are for if/else which will be something like:

$my_string .= ( $append == 'do' ) ? ' there' : '';

so is it possible to do it with only IF and without else?

9 Answers

a simple an very effective way could be to use TRUE FALSE values if you have nothing to return. Not only you have an argument but you can return with exact value you want to, like

$my_string .= ( $append == 'do' ) ? ' there' : true;
Related