OverDocs

Parameter order Edit on GitHub In category: PHP

Introduction #introduction

In its early days PHP was mostly a set functions borrowed from other technologies, such as C or Perl. Unfortunately, there wasn't much attention paid to the consistency of the standard library. It had been assumed that functions' prototypes which are equal to their originals were going to be more intuitive.

It is a sad situation, which is not going to abruptly improve, but you can memorize a simple rule to save your time.

String functions #string-functions

String functions are haystack, needle. There is a very simple way to remember that. Strings can be associated with an alphabet and "h" comes before "n". Voilà!

int strpos  ( string $haystack  , mixed $needle  [, int $offset= 0  ] )
string stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

Array functions #array-functions

Knowing the rule presented above, it is probably the best to remember that parameters for functions operating on arrays are in reverse order.

bool in_array  ( mixed $needle  , array $haystack  [, bool $strict  ] )
mixed array_search  ( mixed $needle  , array $haystack  [, bool $strict  ] )

Comments #comments