2

I've notice some PHP string function are used with underscore and some are not

Here are examples

strlen()
strtolower()
strtoupper()
strpos()

etc..

String function with underscore

str_replace()
str_word_count()
str_split()
str_repeat()

etc..

I expect those function name should be like these

str_len()
str_to_lower()
str_to_upper()
str_pos()
Qirel
  • 25,449
  • 7
  • 45
  • 62
Jaydeep Chauhan
  • 122
  • 2
  • 9
  • 3
    I assume it's because PHP is not a strict language and has had many different developers working on it for multiple years, which leads to such inconsistencies. – Peon Apr 26 '19 at 06:24
  • Yeah...I don't really see any obvious groups of behaviors which would explain the two naming schemes. – Tim Biegeleisen Apr 26 '19 at 06:25
  • 1
    https://stackoverflow.com/questions/20335922/why-is-php-inconsistent etc etc etc etc –  Apr 26 '19 at 06:27
  • They do have some aliases like `sizeof` and `count` so maybe they will use it for having more unified function name. Or not. – Alon Eitan Apr 26 '19 at 06:33
  • does it in the end matter? do you guess what the function manes are? or look them up? –  Apr 26 '19 at 06:37

1 Answers1

1

Many of these functions are named after the C functions which they are patterned after. This isn't specific to string functions; a lot of other basic PHP functions are copied from C programming interfaces: printf(), fopen(), fnmatch()

strlen() is a perfect example. The C function is functionally identical to the PHP function.

strpos() doesn't literally appear in the C standard library, but strstr() and strchr() do; the naming pattern is pretty clear. (The distinction between strstr() and strpos() isn't relevant in C, as returning a pointer to a substring doesn't have any overhead.)

There isn't a strtoupper() or strtolower() in C either, but there is a family of functions which convert strings to various types of numbers (strtol(), strtof(), strtoull(), etc), which may have inspired these names.

As an aside, the old MySQL extension was similarly a direct replica of the MySQL C API, even down to grody details like the name of mysql_real_escape_string().