The Myth of contains in PHP and How to Actually Search Text
The snippet you are looking at relies on syntax that simply does not exist in standard PHP. You cannot write if ($text contains "World"). That construct is valid in languages like Python, Swift, or Ruby, but PHP has its own strict rules for string comparison and substring searching. Trying to run that code will result in a fatal parse error immediately.
To check if a string contains a specific substring in PHP, you must use one of the built-in functions designed for this purpose. The most direct approach is using str_contains(). This function was introduced in PHP 8.0 as part of the modernization of string handling. It returns true if the needle (the search term) is found within the haystack (the target text), and false otherwise.
Here is how you implement that specific logic:
<?php
$text = "Hello World!";
if (str_contains($text, "World")) {
echo "True";
} else {
echo "False";
}
?>
Using str_contains() is the modern standard because it is readable and explicitly handles the boolean return value you need for an if statement. However, understanding the alternatives is crucial when maintaining legacy codebases or running on older servers where PHP 8.0 is not installed.
For environments running PHP 7.x or earlier, str_contains() does not exist. In those cases, developers typically rely on strpos(). The strpos() function returns the numeric position of the first occurrence of a needle in a haystack. If the substring is not found, it returns false. Because this function returns an integer or a boolean false, you cannot simply use it in an if condition without casting it to a boolean.
<?php
$text = "Hello World!";
// Note the cast to (bool) or the !== false check
if (strpos($text, 'World') !== false) {
echo "Found World at position: " . strpos($text, 'World');
}
?>
The trade-off here is readability versus performance and compatibility. While strpos() has been around since PHP's inception and runs on every version, the syntax !== false can sometimes look cluttered compared to the clean str_contains(). Furthermore, if you are building a library or framework that must support PHP 5.x for enterprise clients with old hardware, you are forced to stick with strpos() logic.
Another common pitfall involves case sensitivity. By default, str_contains() and strpos() are case-sensitive. Searching for "world" in "Hello World!" will return false. If your application requires case-insensitive matching, you must normalize the strings first using mb_strtolower() or convert them to ASCII lower-case using strtolower(). This is especially important when dealing with multibyte character sets like UTF-8, where simple strtolower() can sometimes mishandle accented characters depending on the PHP build.
<?php
$text = "Hello World!";
$search = "world"; // lowercase search term
if (str_contains(strtolower($text), strtolower($search))) {
echo "Found (case-insensitive)";
}
?>
If you are working within the Laravel ecosystem, you might find that these low-level functions are often abstracted away by helper methods or Eloquent scopes. For instance, database queries using whereLike handle the containment logic automatically at the SQL level, which is significantly faster for large datasets than iterating through strings in PHP memory.
For more complex scenarios involving regular expressions, such as searching for a pattern rather than an exact substring, you would turn to preg_match(). This offers powerful flexibility but comes with the complexity of learning regex syntax and ensuring your patterns are anchored correctly to avoid performance degradation on large inputs.
Ultimately, choosing the right tool depends on your PHP version and the specific requirements of your search logic. Stick with str_contains() for new projects on modern stacks, and fall back to strpos when necessary, always keeping case sensitivity in mind.