简体   繁体   English

PHP 如果简写

[英]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:现在,我想使用三元运算来执行此操作,但我遇到的所有示例都是针对 if/else 的,类似于:

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

so is it possible to do it with only IF and without else?那么是否有可能只用 IF 而不用别的呢?

Nope.不。 However, the opposite is possible.然而,相反的情况也是可能的。 Here's a quote from the PHP docs:这是 PHP 文档中的引用:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator.自 PHP 5.3 起,可以省略三元运算符的中间部分。 Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.表达式 expr1 ?: 如果 expr1 的计算结果为 TRUE,则 expr3 返回 expr1,否则返回 expr3。

http://php.net/manual/en/language.operators.comparison.php 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.但这实际上只是一个if位移。 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 块

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:您不能在没有 else/false 部分的情况下使用三元,但是如果您的目的是将所有内容都放在一行上,我建议您尝试以下替代方法:

($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.否则将在第一次评估结果为 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.或者您可以做相反的事情,将true if部分留空,并声明 false,正如 Andz 正确指向的那样。

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.如果您反转语句以使三元在赋值语句的外部而不是内部,那么您可以将整个语句放在 TRUE 部分,然后在 ELSE 部分执行一些非操作命令。

a simple an very effective way could be to use TRUE FALSE values if you have nothing to return.如果您没有什么可返回,一个简单而非常有效的方法可能是使用TRUE FALSE值。 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.而不是每次都写出if语句。

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.注意第二个参数是如何引用的,所以它也会在调用代码中改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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