PHP 8 introduces a new function called str_contains
, which makes it easier to check if a string contains a specific substring. This function is an improvement over the older strpos
method, which has been used for a long time to check if a string contains a substring. In this article, we’ll compare the two methods and show you how to use the new str_contains
function.
The old way: strpos
Before the introduction of str_contains
, developers used the strpos
function to check if a string contained a specific substring. Here’s an example of how it was used:
if (strpos($haystack, $needle) !== false) {
echo 'true';
}
In this example, $haystack
is the string to search in, and $needle
is the substring to search for. The strpos
function returns the position of the first occurrence of $needle
in $haystack
, or false
if it’s not found. The !==
operator is used to make sure that false
is not interpreted as the position 0
.
While the strpos
method worked well, it wasn’t always the most intuitive way to check for a substring in a string. Also, the code was often harder to read, especially for less experienced developers.
The new way: str_contains
With PHP 8, a new function called str_contains
was introduced to simplify the process of checking if a string contains a specific substring. Here’s how it works:
if (str_contains($haystack, $needle)) {
echo 'true';
}
As you can see, str_contains
takes two arguments: $haystack
is the string to search in, and $needle
is the substring to search for. The function returns true
if $needle
is found in $haystack
, and false
otherwise.
str_contains
is more intuitive and easier to read compared to strpos
, especially for less experienced developers. Additionally, str_contains
is binary-safe and can perform a case-sensitive search.
Comparing str_contains
and strpos
Let’s compare the two functions by looking at some examples:
Example 1: Searching for an empty string – This is important, pay attention.
$haystack = 'Hello';
$needle = '';
// Using strpos
if (strpos($haystack, $needle) !== false) {
echo 'true';
}
// Using str_contains
if (str_contains($haystack, $needle)) {
echo 'true';
}
Both strpos
and str_contains
will return true
in this example. However, str_contains
is more concise and easier to read. The key takeaway here is we should first check if our substring ($needle) is not empty or you may not get the results that you expected.
Example 2. Making sure our substring is not empty
$haystack = 'Hello';
$needle = '';
// Using strpos
if ($needle && strpos($haystack, $needle) !== false) {
echo 'true';
} else {
echo 'false';
}
// Using str_contains
if ($needle && str_contains($haystack, $needle)) {
echo 'true';
} else {
echo 'false';
}
Both strpos
and str_contains
will return false
in this example. Again, str_contains
is a cleaner and easier to read function when compared against the strpos
function.
Example 3: Searching for a substring
$haystack = 'The quick brown fox jumps over the lazy dog.';
$needle = 'brown';
// Using strpos
if (strpos($haystack, $needle) !== false) {
echo 'true';
}
// Using str_contains
if (str_contains($haystack, $needle)) {
echo 'true';
}
Again, both functions will return true
in this example. However, str_contains
is more readable and intuitive.
Example 4: Case-sensitive search
$haystack = 'The quick brown fox jumps over the lazy dog.';
$needle = 'Brown';
// Using strpos
if (strpos($haystack, $needle) !== false) {
echo 'true';
}
// Using str_contains
if (str_contains($haystack, $needle)) {
echo 'true';
}
In this example, the strpos
function will return false
because it performs a case-sensitive search. As you probably have noticed, str_contains
will also return false
because it’s also case-sensitive.
As you can see, both strpos
and str_contains
have their own strengths and weaknesses. However, str_contains
is more concise, easier to read, and is the recommended way to check if a string contains a specific substring in PHP 8.
Conclusion
In conclusion, PHP 8’s str_contains
function is a significant improvement over the older strpos
method for checking if a string contains a specific substring. It’s more concise, easier to read, and can perform a case-sensitive search. Although strpos
is still a valid way to check for substrings, str_contains
is the recommended method for developers using PHP 8.
Overall, PHP 8 introduces several improvements and new features that make it an excellent choice for developers looking to build robust and secure web applications. By adopting the new str_contains
function, developers can write more efficient and readable code, saving time and effort.
Show us some ❤️
If you found this article useful and informative, please share it with others who may benefit from it. Also, feel free to leave a comment below with your thoughts or any questions you may have.