对于模板引擎,我希望使用速记条件。 我需要检查一个值是否为!= null如果为true,则打印出某行。 我试了一下:速记如果解释?标记运算符
echo "test" ($user->affiliate_id !=null) ?
我不知道背后写?。
对于模板引擎,我希望使用速记条件。 我需要检查一个值是否为!= null如果为true,则打印出某行。 我试了一下:速记如果解释?标记运算符
echo "test" ($user->affiliate_id !=null) ?
我不知道背后写?。
线$someVariable = $condition ? $valueA : $valueB
等同于:
if ($condition) {
$someVariable = $valueA;
}
else {
$someVariable = $valueB;
}
因此,基本上,如果条件是TRUE
,$someVariable
的?
符号之后将采取的第一个值。如果FALSE
,它将采用第二个值(:
符号后面的那个)。
有一种特殊情况,你可以不是定义第一个值,它是这样的:$someVariable = $someValue ?: $someOtherValue
。这相当于:
if ($someValue) {
$someVariable = $someValue;
}
else {
$someVariable = $someOtherValue;
}
所以,如果$someValue
计算结果为TRUE
(比任何0
不同进行评估,以TRUE
值),然后将$someVariable
赶上价值。否则,它会赶上$someOtherValue
。
给你举个例子:
function printGender($gender) {
echo "The user's gender is: " . ($gender == 0 ? "Male" : "Female");
}
printGender(0); // Will print "The user's gender is: Male"
printGender(1); // Will print "The user's gender is: Female"
又如:
function printValuesStrictlyDifferentThanZero($value) {
echo "Value: " . ($value ?: 1);
}
printValuesStrictlyDifferentThanZero(0); // $value evaluates to FALSE, so it echoes 1
printValuesStrictlyDifferentThanZero(1); // $value evaluates to TRUE, so it echoes that $value
编辑:
运营商?:
是不称为ternary operator
。定义三元运算符有多种方法(一个运算符需要三个操作数)。它是一个三元运算符,但不是三元运算符。有些人把它称为三元运算符,因为它们习惯于这样做,并且可能它是PHP中唯一广为人知的三元运算符,但三元运算符比普通运算符更普遍。
它的名字是条件运算符,或更严格地说,三元条件运算符。
让我们假设我定义了一个名为log base
新的运营商,评估,直列,如果一个数$A
与基地$C
对数等于$B
,与它的语法像$correct = $A log $B base $C
并返回TRUE
如果对数是正确的,或者FALSE
如果不是。
当然,该操作是假设的,但它是三元操作符,就像?:
。我将把它称为对数检查运算符。
语法应该是这样的
echo (condition) ? write if true code part here : write else part here
,这意味着在你的情况下,它会像
echo ($user->affiliate_id !=null) ? 'test' : 'not null'
试试这个:
echo ($user->affiliate_id != null)?"test":"";
正如@Federinco提到它的称为三元运算符,官方文档在这里:http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
三元运算符的工作原理是这样:
condition ? true branch : false branch
因此,一个例子是:
$cost = isset($discount) ? $total - $discount : $total;
在上面的例子中,我们检查,如果折扣已定,如果它已经从我们当前的总额中删除了折扣,如果不是我们只是将成本设置为总额。
还有其他一些很酷的东西,你可以用三元运算符不喜欢这样:
$name = $username ?: 'Guest';
在我们正在检查$username
是否存在上面的代码,如果是我们设置$name
到$username
否则我们设置$name
等于Guest
要提醒一句,虽然,三元运算符会导致可读性问题,如果你滥用它,所以要小心,你使用它,没有牺牲的额外的行一个标准的条件语句,如果三元运算符不清楚它在做什么。
它实际上被称为**条件操作符**。是的,它是一个三元操作符(因为它需要三个操作数)。当用作二元运算符(两个操作数)时,它也被称为** Elvis运算符**('':'看起来像文本表情符号)。 –
你想要什么ternary operator
你的代码看起来应该是这样
echo "test" . (($user->affiliate_id !=null) ? 'stringToOutput ifNotNull' : 'stringToOutput if is null');
此外,PHP 7引入了Null coalescing operator,你可以使用。为此,您可以像这样
echo 'test' . ($user->affiliate_id ?? 'ID not found!');
在这种情况下,如果用户$> affiliate_id设置并且不为空,将打印 ,而不是未发现“号!“消息
这就是[三元运算符](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary),只需阅读文档 – Federkun
'$ variable = $ condition ? $ ifTrue:$ ifFalse'等价于if($ condition){$ variable = $ ifTrue; } else {$ variable = $ isFalse; }' –
[PHP的操作符是什么?]可能重复?和“:”叫,他们做什么?](http://stackoverflow.com/questions/1080247/what-are-the-php-operators-and-called-and-what-do-they-do) –