Skip to content
Advertisement

How to reduce the number of if-else statements in PHP?

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?

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;

Are there other tips that can reduce if else statements, especially nested if-else statements?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement