As of right now, three new string functions have been included in the PHP 8. While these three functions do not introduce anything existing built-in functions can’t already accomplish, they are more intuitive.

Functions

str_contains ( string $haystack , string $needle ) : bool

The str_contains function allows you to check if one string is contained within another string. For example:

php > var_dump(str_contains("abc", "b"));
bool(true)

php > var_dump(str_contains("abc", "z"));
bool(false)

Historically, this could be accomplished using the strpos function. strpos will return the position of the first occurrence of the string otherwise it will return FALSE if the string is not contained.

php > var_dump(strpos("abc", "b") !== false);
bool(true)

php > var_dump(strpos("abc", "z") !== false);
bool(false)

The trouble with the existing solutions is that it requires strict comparison which is frequently overlooked. In our example above, strpos would return 0 for a since it’s at position 0 in the string. Without using a strict comparison, the expression would evaluate to false which is clearly not the case.

php > var_dump(strpos("abc", "a") != false);
bool(false)

str_starts_with ( string $haystack , string $needle ) : bool

The str_starts_with function allows you to check if a string begins with another string.

php > var_dump(str_starts_with("abc", "ab"));
bool(true)

php > var_dump(str_starts_with("abc", "xy"));
bool(false)

While str_starts_with too, can be implemented using existing userland functions, the new functions definitely provides a better user experience as its’ intention is more clear.

php > $haystack = "abc";
php > $prefix = "ab";
php > var_dump(substr($haystack, 0, strlen($prefix)) == $prefix);
bool(true)

php > $prefix = "xy";
php > var_dump(substr($haystack, 0, strlen($prefix)) == $prefix);
bool(false)

str_ends_with ( string $haystack , string $needle ) : bool

Similar to str_starts_with, str_ends_with allows you to check if one string ends with another.

php > var_dump(str_ends_with("abc", "bc"));
bool(true)
php > var_dump(str_ends_with("abc", "yz"));
bool(false)

This function can also be implemented similar to str_starts_with using userland functions:

php > $haystack = "abc";
php > $suffix = "bc";
php > var_dump(substr($haystack, -strlen($suffix)) == $suffix);
bool(true)

php > $suffix = "yz";
php > var_dump(substr($haystack, -strlen($suffix)) == $suffix);
bool(false)

What about multibyte strings?

Currently, there are no plans to introduce multibyte versions of these functions. The rationale is that multibyte string functions are for the most part, only needed when dealing with string lengths and offsets. It is expected the new string functions will behave exactly the same with or without multibyte strings.

When can I start using these new string functions?

PHP 8 is scheduled to be released on December 3, 2020. However, using the polyfills below, you can start using them today!

if(!function_exists('str_contains')) {
    function str_contains($haystack, $needle) {
        return strpos($haystack, $needle) !== false;
    }
}

if(!function_exists('str_starts_with')) {
    function str_starts_with($haystack, $needle) {
        return substr($haystack, 0, strlen($needle)) === $needle;
    }
}

if(!function_exists('str_ends_with')) {
    function str_ends_with( $haystack, $needle) {
        return substr($haystack, -strlen($needle)) === $needle;
    }
}

Sources

Author

1 Comment

Write A Comment