Reputation: 9529
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?
Upvotes: 20
Views: 47764
Reputation: 75
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;
Upvotes: 1
Reputation: 9835
You cannot use the ternary without the else/false part, but if your purpose is to have everything on one line I could suggest to try this alternative:
($append == 'do' && $my_string .= ' there');
If the first one is true it will proceed with the second statement. Otherwise will just stop after the first evaluation resulting false.
Refer to the lazy evaluation (the so called Short-circuit evaluation)
Or you can do the opposite, leaving the true if part empty, and declare just the false, as Andz correctly points to.
Upvotes: 4
Reputation: 3973
if you are considering to make the code shorter you can write the same thing on a single line.
if ($append == 'do') {$my_string .= ' there';}
EDIT
I just discovered this and thought come in handy. You can also write the if block like this
if ($append == 'do') $my_string .= ' there';
Upvotes: 4
Reputation: 145482
An alternative way to check for a condition and then append to the string would be:
($append == 'do') and ($my_string .= ' there');
But that's really just an if
displacement then. But comes close to an "ternary without the else".
Upvotes: 5
Reputation: 9857
You could create a function that does what you want. Instead of writing out the if
statement each time.
function doAppend($doIt, &$value, $appendValue)
{
if($doIt) $value .= $appendValue;
}
Call it with:
doAppend($append == 'do', $my_string, ' there');
Note how the second parameter is by reference, so it will be changed in the calling code too.
Upvotes: 1
Reputation: 9322
combining mellamokb and Andz you can do this:
( $append != 'do' ) ?: $my_string .= ' there';
Upvotes: 20
Reputation: 56769
You can do this:
( $append == 'do' ) ? $my_string .= ' there' : $noop ;
If you invert the statement so that the ternary is on the outside instead of the inside of the assignment, then you can put an entire statement in the TRUE part, then just do something that is a no-operation command in the ELSE part.
Upvotes: 4
Reputation: 2258
Nope. However, the opposite is possible. Here's a quote from the PHP docs:
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.
http://php.net/manual/en/language.operators.comparison.php
Upvotes: 28
Reputation: 2344
No... ternary means three parts you need the condition, the true part, and the false part
Upvotes: 23