php - 可以用三进制写吗?
问题描述
//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;
}
我想知道上面的代码是否可以在一行中用三进制编写
解决方案
从技术上讲,是的:
is_array($data[0]) ? $datas = $data : $datas[0] = $data;
但是不要。
三元运算符旨在表达操作:
something = condition ? value : other-value
将它们用于:
condition ? operation : other-operation
……充其量是令人困惑的。
如果您有某种if/else
情况,请使用if/else
语句。
推荐阅读
- javascript - 如何检查链接的功能数量?
- scala - Scala 3 TypeTest over Union 似乎无法正常工作
- linux - 处理linux内核驱动程序中的几个中断
- android - 在 Paging 3 库 android 中找不到类 PagingRx
- python - BSC 代币浏览器
- sql - postgres 表的分区键失败错误
- java - 如何将列表重置为其初始状态
- java - 在android中按下按钮时进行HTTP Get调用
- spring - 在 JPA Spring Boot 中保持多对一关系
- amazon-web-services - 如何迭代 Terraform 中的“计数”资源?