php PHP问号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6583673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:47:33  来源:igfitidea点击:

PHP question mark

php

提问by user829814

$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ? 'style="display:none;"' : '';

Can anyone explain to me what that question mark does in this line of code? Thanks a lot!

谁能向我解释这个问号在这行代码中的作用?非常感谢!

回答by nickf

This is called the Ternary Operator, and it's common to several languages, including PHP, Javascript, Python, Ruby...

这称为三元运算符,它在多种语言中都很常见,包括 PHP、Javascript、Python、Ruby...

$x = $condition ? $trueVal : $falseVal;

// same as:

if ($condition) {
    $x = $trueVal;
} else {
    $x = $falseVal;
}

One very important point to note, when using a ternary in PHP is this:

需要注意的一个非常重要的点,在 PHP 中使用三元时是这样的:

Note: Please note that the ternary operator is a statement, and that it doesn't evaluate to a variable, but to the result of a statement. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions. source

注意:请注意,三元运算符是一个语句,它的计算结果不是变量,而是语句的结果。了解是否要通过引用返回变量很重要。语句返回 $var == 42 ? $a : $b; 因此,在按引用返回的函数中将不起作用,并且在更高的 PHP 版本中会发出警告。 来源

回答by Ovais Khatri

Actually this statment is representing a Ternary operation, a conditional expression:

实际上这个语句代表一个三元运算,一个条件表达式:

// works like:    (condition) ? if-true : if-false;

$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ?  'style="display:none;"':'';

in your case $hideCodewill have style="display:none;"value if

在你的情况下$hideCodestyle="display:none;"有价值,如果

$likesObj->isAlreadyLikedByUser(facebookUID())

will return true, else it will be null or empty.

将返回 true,否则它将为 null 或为空。

回答by Patrick Desjardins

It's a shorter version of a IF statement.

它是 IF 语句的较短版本。

$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ? ' style="display:none;"':'';

if in fact :

如果事实上:

if($likesObj->isAlreadyLikedByUser(facebookUID()))
{
   $hideCode = 'style="display:none"';
}
else
{
 $hideCode = "";
}

For the purism :

对于纯粹主义:

It's representing a Ternary operation

它代表一个三元操作

回答by Aleks G

This is the simple if-then-else type logic:

这是简单的 if-then-else 类型逻辑:

(condition) ? (if-true-value) : (if-false-value)

so in your case, the condition is checked (i.e. has the page already been liked by the user); if yes (true condition), then style="display:none;"is printed so that whatever the element you're using is not displayed. Otherwise, an empty string is printed, which is the equivalent of not printing anything at all, naturally.

所以在你的情况下,条件被检查(即页面已经被用户喜欢);如果是(真条件),则style="display:none;"打印,以便不显示您使用的任何元素。否则,打印一个空字符串,这相当于根本不打印任何东西,自然。

回答by robamaton

It's part of a ternary operator. The first part is the conditional of an if-else statement. After the question mark is the "if" block, and after the colon is the "else" block.

它是三元运算符的一部分。第一部分是 if-else 语句的条件。问号后面是“if”块,冒号后面是“else”块。

回答by Nicola Peluchetti

It is the ternary operator: it means

它是三元运算符:这意味着

if $likesObj->isAlreadyLikedByUser(facebookUID())is true assign style="display:none;to the variable, otherwise assign ''

如果$likesObj->isAlreadyLikedByUser(facebookUID())为真赋值 style="display:none;给变量,否则赋值''

回答by Webberman

$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ? 'style="display:none;"':'';

$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ?'style="display:none;"':'';

This is the same as the following:

这与以下内容相同:

if ($likesObj->isAlreadyLikedByUser(facebookUID()))
{
    $hideCode = 'style="display:none;"';
}
else
{
    $hideCode = '';
}

回答by Zeta Two

This is a very compact if-clause.

这是一个非常紧凑的 if 子句。

(condition) ? :

(健康)状况) ?: