- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
寻找以下三元语句语法中的哪一个(如果有的话)符合 PSR-2 - 我还需要指向文档或一些权威链接:
$error = ($error_status) ? '错误' : '没有错误';
或
$error = $error_status ? '错误' : '没有错误';
注意: php.net它显示了带括号的语法,但我无法在任何“官方 PSR-2”文档中找到它。
如果没有关于此的 PSR-2 标准,哪种方式是最常见的约定?
最佳答案
The PSR-2 standard特别省略对运营商的任何意见:
There are many elements of style and practice intentionally omitted by this guide. These include but are not limited to: ... Operators and assignment
由于括号用于对表达式进行分组,因此您的示例没有多大意义:
$error = ($error_status) ? 'Error' : 'No Error';
此处将单个变量括在括号中没有任何意义。更复杂的条件可能会受益于括号,但在大多数情况下,它们只是为了提高可读性。
一个更常见的模式是始终包围整个三元表达式:
$error = ($error_status ? 'Error' : 'No Error');
这样做的主要动机是 PHP 中的三元运算符具有相当笨拙的关联性和优先级,因此在复杂表达式中使用它通常会产生意想不到的/无用的结果。
常见的情况是字符串连接,例如:
$error = 'Status: ' . $error_status ? 'Error' : 'No Error';
此处连接(.
运算符)实际上在三元运算符之前求值,因此条件始终为非空字符串(开始 'Status: '
),你将始终得到字符串 Error'
作为结果。
括号是防止这种情况所必需的:
$error = 'Status: ' . ($error_status ? 'Error' : 'No Error');
当“堆叠”三元表达式以形成 if-elseif 链的等价物时,存在类似的情况,因为 PHP 历史早期的一个错误意味着多个三元运算符是按从左到右的顺序计算的,而不是将整个假分支缩短当条件为真时。
来自 the PHP manual 的示例更清楚地解释这一点:
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');
// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right
// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');
// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
关于php - PSR-2 PHP 三元语法中是否需要括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26389123/
我想知道javascript中if的简码是什么? 就像在 PHP 中一样: $res = ($x > $y)? $x: $y; 它在 JavaScript 中的转换是什么? 最佳答案 在 javasc
请问为什么下面的代码会报错? 错误: numberOne > numberTwo ? return "true" : return "false"; ^
在我的代码中,我检查系统函数是否等于零,如果是我返回另一个值,如果不是,我返回测试值。 (class.verylongfunc(arg, arg) == 0) ? othervar : cla
在 PHP 中,有没有一种方法可以使用三元条件连接两个字符串? 当我尝试这样做时,我得到的只是 else 而不是所需的 something else。 最佳答案 像这样把整个三元运算符放在方括号中:
似乎在三元运算符中存在某种类型混淆。我知道这已在其他 SO 线程中得到解决,但它始终与可空值有关。另外,就我而言,我真的只是在寻找更好的方法。 我希望能够使用 proc.Parameters[PARA
有没有办法在不进行赋值或伪造赋值的情况下进行 java 三元运算? 我喜欢在执行一堆 if/then/else 时的简洁三元代码。 我希望能够基于 boolean 代数语句调用两个 void 函数之一
我正在使用 XSLT 和 XML 来生成输出文档。 我在数据中拥有的(以我无法控制的 XML 形式)如下: 4 我需要在计算中使用这些。我看到为这些提供默认值需要对文档执行转换以提供一个有点冗长的
这个问题已经有答案了: Ternary operators in JavaScript without an "else" (13 个回答) 已关闭 4 年前。 我一直使用这样的三元表达式,但我不喜欢
我在 VB.NET 中发现了一个可以轻松重现的简单错误: Dim pDate As Date? Dim pString As String = "" ' works fine as expected
所以,我有这段代码,它实际上有效: (散列将是这样的对象:{"bob"=> "12, "Roger"=> "15", etc},并且 isGood(key) 是调用函数 isGood ,如果玩家好或坏
是否有以下 JavaScript bool 三元表达式的简写语法: var foo = (expression) ? true : false 最佳答案 当然,您只想将表达式转换为 bool 值: v
在 Java 中,如果我在常规 if 中使用三元 if 运算符,例如: if ((x > y - z) ? true : callLongWaitedMethod(many, parameteres)
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
var test = "Hello World!"; 在 Java 10+ 中,上面的代码片段可以编译,test 在编译时被推断为 String。 但是,我们可以使用条件(三元)运算符来返回不同的类型
嗨,我尝试在渲染内部使用三元条件,但遇到一些错误,这是我的代码: render() { return ( (this.emai
这里我有以下 JavaScript 代码,带有两个值。 var w = $("#id1").val(); var h = $("#id2").val(); (w == h) ? (w=350 , h
我一直想知道如何用 C++ 兼容语言编写 "A ? B : C" 语法。 我认为它的工作方式类似于:(伪代码) If A > B C = A Else C = B 有没有经验丰富的 C++
考虑两个 vector ,A 和 B,大小为 n,7 <= n <= 23 . A 和B 都只包含-1、0 和1。 我需要一个计算A 和B 内积的快速算法。 到目前为止,我一直在考虑使用以下编码将
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
如果您一开始就讨厌三元条件运算符,则无需回复 ;) 我经常看到它与赋值表达式一起使用,例如: var foo = (some_condition) ? then_code : else_code; 但
我是一名优秀的程序员,十分优秀!