[英]Better way of PHP if shorthand
I was wondering whether there's a better way of writing this? 我想知道是否有更好的书写方式? I am aware that the answer could be no. 我知道答案可能不是。
$variable = aMethodToGetSomeValue();
$result = ($variable) ? $variable : null/false/0;
Basically, I want to set the result to $variable
if $variable
is not false, else return as defined. 基本上,如果$variable
不为false,我想将结果设置为$variable
,否则按定义返回。
The above code works, the question is just whether there's a better way of writing the shorthand? 上面的代码有效,问题是是否存在一种更好的速记方式? Since the use case for me is such that the $variable
is a query to the database. 由于对我而言, $variable
是对数据库的查询。
Similar question but not really what I'm asking for I believe..? 类似的问题,但我并不是真的希望我相信..?
Look up shorthand ternary if PHP >= 5.3 如果PHP> = 5.3,则查找速记三进制
Example: 例:
<?php
$e = "asdf";
$e = ($e) ?: "foo";
var_dump($e); // string(4) "asdf"
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.