如何减少PHP中if-else语句的数量? [英] How to reduce the number of if-else statements in PHP?

查看:431
本文介绍了如何减少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语句的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆