PHP:不使用 else 使用简短的 if 语句?

标签 php if-statement ternary-operator

我是 if 简短 if 版本的粉丝,例如:

($thisVar == $thatVar ? doThis() : doThat());

不过我想删除 else 语句,例如:

($thisVar == $thatVar ? doThis());

但是,它不会起作用。有没有我错过的方法?

最佳答案

没有 else 就不能使用它。但你可以试试这个:

($thisVar != $thatVar ?: doThis());

if ($thisVar == $thatVar) doThis();

关于PHP:不使用 else 使用简短的 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48777723/

相关文章:

php - 通过比较值 PHP 从两个不同的数组创建一个大数组

java - 您好,我似乎无法打印使用 DrawingPanel 比较三个圆圈大小的程序的结果

c - 是否保证解析为 "(a ? b : (c ? d : e))"?

PHP 多个三元运算符未按预期工作

c# - 为什么 C# 三元运算符不能与委托(delegate)一起工作?

php - 查询循环 php 中的查询

php - 从 php 发送 json 数据

php - 如何在 php 代码中的 css 类上应用 if-else?

c++ - 对一组函数使用 C++ 函数模板

php - Laravel 4 - 全局声明类?