Home > Article > Backend Development > Practical switch commands in PHP
In PHP, the switch
statement is the control program A powerful tool for workflows, especially when dealing with multiple conditional branches. However, achieving clean and efficient conversions comes with its own obstacles. This article explores basic tips and practices for checking the quality of the "switch" command to ensure code quality and optimize its performance.
The first thing to consider is the switch
statement Whether to include the default
case. Adefault
acts as a fallback option, ensuring that code is executed when there is no defined case
match.
default
should always be present. In fact, the command match
is an alternative version of switch
, when it fails to match anythingcase
, and default
is missing, it throws an exception.
<?php switch($a) { case 0: echo 'A'; break; case 1: echo 'A'; break; } ?>
default
Serves a different purpose:
switch
ensures that a limited list of cases is processed. Then, an exception should be thrown whenever a situation other than a legal situation reaches the command.
<?php switch($a) { case 0: echo 'A'; break; case 1: echo 'A'; break; default: throw new MyException('This should be zero or one'); } ?>
Always make sure the switch has a default value.
检查switch
陈述中是否有重复案例是有回报的。PHP 只处理它遇到的第一种情况,忽略其他情况。这会导致死代码,开关中的一些分支将被忽略并且永远不会被使用。
<?php switch($a) { case 'gif': // doSomething() and break case 'jpeg': case 'jpg': // doSomething() and break case 'png': // doSomething() and break case 'gif': // doSomething() and break default: throw new MyException('This should be an image format'); } ?>
重复的情况经常发生在很长的案例列表中。
当实际值被常量名称或 PHP 的类型杂耍隐藏时,也会发生这种情况。确实,switch
使用了宽松的比较风格,有些文字实际上可能是相同的。
<?php // $a in [null, false, '', 0] switch($a) { // Most of the cases ends here, except 0 // because it is the first encountered case '': print 'Empty string';break; // 0 goes here case 0: print 'Zero';break; case false: print 'false';break; case null: print 'null';break; default: throw new MyException('This should be empty'); } ?>
仔细检查案例值并删除重复项。
另一个常见问题是不同 case 分支中的代码块重复。这种冗余会使您的系统变得臃肿switch
,并且使将来的更新变得困难。考虑重构代码以消除重复并合并案例。
<?php switch($a) { case 'john': $b = 0; break; case 'henry': $b = 10; break; // duplicate of case 'john' case 'julia': $b = 0; break; case 'jeroen': $b = 30; break; default: throw new MyException('This is an unexpected name'); } ?>
PHP 内置了对枚举的支持。枚举的重要优点之一是其有限性。它们代表选项的完整集合,并且不存在其他选项。这使得检查 a 是否switch
涵盖了所有可能的情况变得很容易。
<?php enum formats { case GIF; case JPG; case PNG; } switch($a) { case formats::GIF: // doSomething() and break; case formats::PNG: // doSomething() and break; // We are missing one case : JPG! // Unknown cases triggers an exception lazily (only if reached) case formats::TIFF: // doSomething() and break; default: throw new MyException('Unknown image format'); } ?>
PHP 7.2 引入了针对switch
.
在此之前,PHP 会逐个检查每个案例,并在找到匹配值后立即停止。在 PHP 7.2 及更高版本中,当大小写值是简单文字时,PHP 会为它们设置一个查找表。然后,PHP 立即跳转到正确的情况,并绕过所有其他不匹配的情况。
<?php switch($a) { case 1: // doSomething() and break; case 2: // doSomething() and break; default: throw new MyException('Unknown image format'); } ?>
当所有情况都是简单值时,就会发生简单切换,可以直接与传入变量进行比较。
如果开关混合了表达式和简单值,建议将简单文字放在第一个开关中switch
,并将其他文字放在第二个开关中。
<?php switch($a) { case 1: // doSomething() and break; case 2: // doSomething() and break; default: switch($a) { case $b + 2: // doSomething() and break; case strtolower($a): // doSomething() and break; default: throw new MyException('Unknown case'); } } ?>
以不同的方式处理它们也可能更方便,例如使用先前的条件。
In short, switch
well-structured statements in PHP can greatly improve the clarity of the code degree, maintainability and performance. By checking for missing default entries, eliminating duplicate cases and blocks of code, leveraging enum-like structures, and optimizing complex switches, you can ensure that your statements are still switch
An efficient and effective part of any PHP code base.
The above is the detailed content of Practical switch commands in PHP. For more information, please follow other related articles on the PHP Chinese website!