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 语句的数量?
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
