首页 > 解决方案 > 可以用三进制写吗?

问题描述

//if more than one attribute is being added
if (is_array($data[0])) 
{
    $datas = $data;    
}
//if only one attribute is being added
else
{
    $datas[0] = $data;
}

我想知道上面的代码是否可以在一行中用三进制编写

标签: phpternary

解决方案


从技术上讲,是的:

is_array($data[0]) ? $datas = $data : $datas[0] = $data;

但是不要

三元运算符旨在表达操作:

something = condition ? value : other-value

将它们用于:

condition ? operation : other-operation

……充其量是令人困惑的。

如果您有某种if/else情况,请使用if/else语句。


推荐阅读