.. _structures-switchtoswitch: .. _switch-to-switch: Switch To Switch ++++++++++++++++ The following structures are based on if / elseif / else. Since they have more than three conditions (not withstanding the final else), it is recommended to use the switch structure, so as to make this more readable. On the other hand, `switch() `_ structures with less than 3 elements should be expressed as a if / else structure. Note that if condition that uses strict typing (=== or !==) can't be converted to `switch() `_ as the latter only performs == or != comparisons. Note that simple switch statement, which compare a variable to a literal are optimised in PHP 7.2 and more recent. This gives a nice performance boost, and keep code readable. .. code-block:: php See also `PHP 7.2's switch optimisations `_ and `Is Your Code Readable By Humans? Cognitive Complexity Tells You `_. Connex PHP features ------------------- + `switch `_ + `match `_ Suggestions ___________ * Use a switch statement, rather than a long string of if/else * Use a match() statement, rather than a long string of if/else (PHP 8.0 +) Specs _____ +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Short name | Structures/SwitchToSwitch | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Rulesets | :ref:`All `, :ref:`Analyze `, :ref:`Changed Behavior ` | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Exakat since | 0.8.4 | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | PHP Version | All | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Severity | Minor | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Time To Fix | Quick (30 mins) | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Precision | High | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Examples | :ref:`case-thelia-structures-switchtoswitch`, :ref:`case-xoops-structures-switchtoswitch` | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Available in | `Entreprise Edition `_, `Exakat Cloud `_ | +--------------+-------------------------------------------------------------------------------------------------------------------------+