Worldscope

PHP STR_CONTAINS()

Palavras-chave:

Publicado em: 02/08/2025

Understanding and Using PHP's str_contains() Function

This article delves into the str_contains() function in PHP, which provides a straightforward way to check if a string contains a specific substring. We'll explore its usage, implementation details, and alternative methods for achieving the same result.

Fundamental Concepts / Prerequisites

To fully grasp the concepts discussed in this article, a basic understanding of PHP strings and functions is necessary. Familiarity with conditional statements (e.g., if statements) will also be beneficial. Specifically, understand that PHP strings are sequences of characters and that functions encapsulate reusable blocks of code.

Core Implementation/Solution

The str_contains() function simplifies the process of checking for the presence of a substring within a larger string. Here's a code example demonstrating its use:


<?php

/**
 * Checks if a string contains a specific substring.
 *
 * @param string $haystack The string to search in.
 * @param string $needle   The substring to search for.
 *
 * @return bool True if the substring is found, false otherwise.
 */
function check_string_contains(string $haystack, string $needle): bool {
  return str_contains($haystack, $needle);
}

// Example Usage:
$string = "This is a sample string.";
$substring = "sample";

if (check_string_contains($string, $substring)) {
  echo "The string contains the substring.";
} else {
  echo "The string does not contain the substring.";
}

echo "<br>"; // Add a line break for better formatting

$string2 = "Another example.";
$substring2 = "missing";

if (check_string_contains($string2, $substring2)) {
  echo "The string contains the substring.";
} else {
  echo "The string does not contain the substring.";
}

?>

Code Explanation

The code snippet defines a function check_string_contains that accepts two string arguments: $haystack (the string to search within) and $needle (the substring to search for). The function then utilizes the built-in str_contains() function. str_contains($haystack, $needle) returns true if $needle is found within $haystack, and false otherwise. The code then provides two example usages, demonstrating how to use the function and display appropriate messages based on the result.

Complexity Analysis

The str_contains() function in PHP generally has a time complexity of O(m*n) in the worst case, where 'm' is the length of the needle (substring) and 'n' is the length of the haystack (main string). This is because, in the worst case, the function might have to compare the needle against every possible substring of the haystack. The underlying implementation might use algorithms like the naive string search or potentially optimized algorithms (depending on the PHP version), which can improve the average-case performance. The space complexity is O(1) as it performs the search in place and doesn't require significant additional memory proportional to the input size.

Alternative Approaches

Before str_contains() was introduced in PHP 8.0, the strpos() function was commonly used to achieve similar functionality. strpos() returns the position of the first occurrence of a substring in a string, or false if the substring is not found. An alternative implementation could look like this:


<?php

function check_string_contains_strpos(string $haystack, string $needle): bool {
  return strpos($haystack, $needle) !== false;
}

?>

The trade-off here is that strpos() returns the position of the substring, which can be useful in some scenarios, but it requires a comparison with false to determine the boolean result. str_contains() is more direct and readable when simply checking for the existence of a substring.

Conclusion

The str_contains() function provides a clean and efficient way to determine if a string contains a specific substring in PHP. While alternative methods like using strpos() exist, str_contains() offers improved readability and simplicity for simple substring existence checks. Understanding its usage and potential alternatives allows developers to choose the most appropriate method for their specific needs.