.. _structures-nosubstrone: .. _avoid-substr()-one: Avoid Substr() One ++++++++++++++++++ Use array notation ``$string[$position]`` to reach a single byte in a string. There are two ways to access a byte in a string : `substr() `_ and ``$v[$pos]``. The second style is more readable. It may be up to four times faster, though it is a micro-optimization. It is recommended to use it. PHP 7.1 also introduces the support of negative offsets as string index : negative offset are also reported. Beware that `substr() `_ and ``$v[$pos]`` are similar, while `mb_substr() `_ is not. The first function works on bytes, while the latter works on characters. .. code-block:: php Suggestions ___________ * Replace substr() with the array notations for strings. * Replace substr() with a call to mb_substr(). Specs _____ +--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Short name | Structures/NoSubstrOne | +--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Rulesets | :ref:`All `, :ref:`Analyze `, :ref:`CE `, :ref:`CI-checks `, :ref:`Changed Behavior `, :ref:`CompatibilityPHP71 `, :ref:`Performances `, :ref:`Suggestions `, :ref:`Top10 ` | +--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Exakat since | 0.8.4 | +--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | PHP Version | All | +--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Severity | Minor | +--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Time To Fix | Instant (5 mins) | +--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Precision | Very high | +--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Examples | :ref:`case-churchcrm-structures-nosubstrone`, :ref:`case-livezilla-structures-nosubstrone` | +--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Available in | `Entreprise Edition `_, `Community Edition `_, `Exakat Cloud `_ | +--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+