A lot of defenders of PHP will say that the language has improved immensely in later versions with better support for OOP, which it has! However, PHP is fundamentally broken in its implementations of its stock functions, even compared to other dynamically-typed languages such as JavaScript. Let’s look at some examples:
PHP - Functions of a String
Function | Prefix | String is first parameter? | Descriptive Name |
---|---|---|---|
explode | ❌ | ❌ | Sorta |
ord | ❌ | ❌ | ❌ |
strstr | str | ✅ | ❌ |
str_pad | str_ | ✅ | ✅ |
substr | ❌ | ✅ | ✅ |
There’s a great amount of inconsistency here, especially with how the functions are named. For newcomers to PHP or the souls damned to hell who have to maintain WordPress plugin source code, this can be quite a barrier to writing effective code.
This isn’t even mentioning the function aliases, of which there are many: often nonsensical. For example, join
is an alias of implode
, which is one of the more sensible ones; programmers migrating from other languages may be more familiar with join
/split
as opposed to implode
/explode
as PHP eccentrically uses.
One might expect strlen
to provide the number of characters in a string, as it does in C++ , but it instead returns the number of bytes in a given string, causing problems for unicode multi-byte characters.
Additionally, it can be difficult to find the correct functions for the task at hand, and this applies to many areas of PHP stock functions. The prefixes for these are incredibly inconsistent, some beginning with str
, some beginning with str_
, some instead ending with str
, and some with no mention of a string whatsoever. A much more fitting approach would be what we see in the following table of analogous JavaScript string functions.
JS - Functions of a String
Function | Prefix | Member of String object? | Descriptive Name |
---|---|---|---|
String.split | ❌ | ✅ | ✅ |
String.charCodeAt | ❌ | ✅ | ✅ |
String.indexOf | ❌ | ✅ | ✅ |
String.padLeft | ❌ | ✅ | ✅ |
String.substring | ❌ | ✅ | ✅ |