我试图用PHP来定位自己,它来自Perl、R、C和其他几种语言。
问题:
发布于 2015-10-26 17:25:10
1.
在perl中,您可以将?:看作是||,但它实际上是三元/(三叉的?)运算符,其中省略了第二个参数,并包含第一个参数($x)。
$y= $x ?: "I am undefined"; # not before v5.3.x您不能使用($x==1) or return|break|continue;,所以您必须使用常规的if条件(对于单个语句可以省略大括号)
if (!($x==1)) break;至于链式三元,如果你想让它像perl那样工作,恐怕事情并不像你想的那样简单。
$y = ($x1==1 ? 2 :
($x2==2 ? 3 :
($x3==3 ? 2 : 5
))); // close as many times as there is rows above如果变量不插入双引号,则可以使用大括号,
print "Val: {$array[$x]}";2.
至于你可以用的语法糖
$arg += array(
"default" => 55,
);就像你会
%arg = ("default" => 55, %arg);在perl中,为了给出缺少散列键的默认值。
https://stackoverflow.com/questions/33350058
复制相似问题