我正在寻找一些缩写的if/else代码,但与$var ? $a : $b
不同的是,它不需要类似于“else”的返回值。我想要的基本上就是这样,但更短:
$myVariable = "abc";
echo $myVariable ? $myVariable : "hello";
echo $myVariable ? "hello" : $myVariable;
我在Lua中已经习惯这样做:
local myVariable = "abc"
-- In case myVariable is false, print "hello". Otherwise it prints "abc"
print ( myVariable or "hello" )
-- In case myVariable does have something (So, true) print "goodday."
print ( myVariable and "goodday" )
所以我想知道,PHP是否有这样的功能呢?谢谢。
if($myVariable)echo $myVariable ;
吗? - NullPoiиteя