How to reduce the number of if-else statements in PHP?(如何减少 PHP 中 if-else 语句的数量?)
问题描述
我发现有很多 if-else 语句,尤其是嵌套的 if else 语句,这些语句使我的代码可读性较差.PHP中如何减少if else语句的数量?
I found that there are many if-else statements, especially nested if else statements, these statements make my code less readable. How to reduce the number of if else statements in PHP?
我的建议如下:1.在合适的时候使用switch语句;2.可行时使用exit()语句;3. 可行时使用三元语句;
My tips are as follows: 1.Use a switch statement when it is suitable; 2.use exit() statement when it is feasible; 3. Use ternary statement when it is feasible;
是否有其他技巧可以减少 if else 语句,尤其是嵌套的 if-else 语句?
Are there other tips that can reduce if else statements, especially nested if-else statements?
推荐答案
将您的代码重构为更小的工作单元.太多的条件逻辑是一种代码味道,通常表明您的函数需要重构.
Refactor your code into smaller work units. Too much conditional logic is a code-smell and usually indicates that your function needs to be refactored.
这篇关于如何减少 PHP 中 if-else 语句的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何减少 PHP 中 if-else 语句的数量?


- 从 PHP 中的输入表单获取日期 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Laravel 仓库 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01