Search code examples
phpapi-designlanguage-design

Why is the order of haystack and needle inconsistent in PHP functions in_array and strpos?


The order of parameters haystack and needle in these 2 very core PHP functions is not consistent:

Why is this the case?


Solution

  • Needle/Haystack in PHP

    They are actually not inconsistent like many think. You have partly discovered the consistency:

    • All string functions are (haystack, needle)

    • All array functions are (needle, haystack)

    Rasmus Lerdorf (the creator of PHP) states this in a talk in 2019 around 25 minutes in:

    25 Years of PHP - phpday 2019

    https://youtu.be/iGOAQli7tGc?t=1485

    Inconsistencies in PHP

    At 16:45, he has a slide titled "What was he thinking?" and he addresses "naming inconsistencies". (needle/haystack issue is just one of them)

    https://youtu.be/iGOAQli7tGc?t=1005

    In this talk he also explains that his design decisions for PHP are almost all based on the idea that PHP is a "wrapper for underlying C libraries", and has kept the parameters in the same order as the underlying code.