.. _performances-logicaltoinarray: .. _logical-to-in\_array: Logical To in_array +++++++++++++++++++ Multiple exclusive comparisons with ``or``` may be replaced by faster alternative. + `isset() `_ and an array which keys are the target comparisons + `array_key_exists() `_ and an array which keys are the target comparisons + `strpos() `_ call, with all the target values merged into a string + `str_contains() `_ call, with all the target values merged into a string + `switch() `_ call, with each case being an assignation + `match() `_ call + `in_array() `_ call, with each values in an array While each alternative has its performance gain, they make the code more readable by bringing the alternative values into one simple list. As little as three ``or`` comparisons are slower than using an alternative. The more calls, the slower is as string of ``or``. Also, the further the target value is in the ``or`` list, the slower it is to find it. Although, it is not easy to control that value. This analysis also reports `in_array() `_ calls with arrays of a single element : those should be turned into a ``or`` call, or have more values in the array, or have the array published as a constant. This is a micro-optimisation : speed gain is low, and marginal. Code centralisation is a more significant advantage. Thanks to `Frederic Bouchery `_ for extending the alternatives of that analysis. .. code-block:: php true, default => false }; // in_array() // Set the list of alternative in a variable, property or constant. $result = in_array($a, $valid_values, true); // use third argument when you can // slowest and hard to read $result = $a == 'a' || $a == 'b' || $a == 'c' || $a == 'd'); ?> See also `in_array() `_, `isset() `_, `match() `_, `switch() `_ and `strpos() `_. Suggestions ___________ * Replace the list of comparisons with a in_array() call on an array filled with the various values * Replace the list of comparisons with a strpos() call on an string joined with the various values * Replace the list of comparisons with a match() call on an string joined with the various values * Replace the list of comparisons with a switch() call on an string joined with the various values * Replace the list of comparisons with a isset() call on a hash whose keys are the various values Specs _____ +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Short name | Performances/LogicalToInArray | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Rulesets | :ref:`All `, :ref:`Analyze `, :ref:`Changed Behavior ` | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Exakat since | 0.12.5 | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | PHP Version | All | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Severity | Minor | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Time To Fix | Quick (30 mins) | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Precision | Very high | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Examples | :ref:`case-zencart-performances-logicaltoinarray` | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Available in | `Entreprise Edition `_, `Exakat Cloud `_ | +--------------+-------------------------------------------------------------------------------------------------------------------------+