Task #37784

Evaluate whether using substr_compare() instead substr()===

Added by Adrian Föder over 9 years ago. Updated almost 9 years ago.

Could have
Target version:
Start date:
Due date:
% Done:


Estimated time:
PHP Version:
Has patch:


consider using http://php.net/manual/function.substr-compare.php globally as preference over substr()

As a side note I'd still suggest introducing a Utilities method for comparing the beginning sequence, in order to avoid the pretty complicated and unintuitive handling of the substr_compare return values.


Updated by Christian Müller over 9 years ago

we should maybe make a quick statistic about speed in static call to Utility function vs. substr vs. substr_compare


Updated by Karsten Dambekalns over 9 years ago

  • Status changed from New to Accepted
  • Assignee set to Karsten Dambekalns

Updated by Karsten Dambekalns over 9 years ago

  • Status changed from Accepted to Needs Feedback

Adrian, why did you suggest this? I made a quick and dirty comparison, and substr_compare() seems to be significantly slower. Feel free to have a look at https://gist.github.com/3098900 and see if my test makes sense.

Also, the use of substr_compare() is everything but intuitive…


Updated by Adrian Föder over 9 years ago

Karsten, thanks for actually checking this; the reason why I suggested this was because of http://www.rvantwisk.nl/flow3-irc-log/2012/june/6.html 08:32 ;-)

Without reading the log further, I initially thought about a t3lib_div::isFirstPartOfStr derivate in FLOW3 that checks for the beginning of a string or, like your gist recommends, for the ending.
Could be kinda Utility\Strings::beginsWith('TYPO3_FLOW3_Foo', 'TYPO3') or ::endsWith().
This would increase code readability fantastically IMO in order to avoid constructs like strpos($foo, 0, strlen('TYPO3')), if that is right (pseudo-code).


Updated by Adrian Föder almost 9 years ago

  • Status changed from Needs Feedback to Rejected

Also available in: Atom PDF