简体   繁体   中英

PHP if shorthand

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?

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

不......三元意味着你需要条件的三部分,真实的部分和错误的部分

结合 mellamokb 和 Andz 你可以这样做:

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

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

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';

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.

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.

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM