PHP Tutorial PHP Advanced PHP Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

PHP - String str_contains() Function



The PHP String str_contains() function is used to check if a string contains a given substring. It performs case-sensitive check and returns true if the string contains specified substring, else returns false.

Note: It is a binary-safe function. This function is new in PHP 8.

Syntax

str_contains(str1, str2)

Parameters

str1 Required. Specify the string to check
str2 Required. Specify the substring to check whether the string contains it or not.

Return Value

Returns true if the string contains specified substring, else returns false.

Example:

In the example below, str_contains() function is used to check whether the given string contains specified substring or not.

<?php
$str1 = "Hello";

//checking whether $str1 contains empty string
if(str_contains($str1, "")) {
  echo "Checking for empty string always returns true.\n";
}

//checking whether $str1 contains "ll"
if(str_contains($str1, "He")) {
  echo "$str1 contains 'll'.\n";
}

//case-sensitive check -checking 
//whether $str1 contains "LL"
if(!str_contains($str1, "LL")) {
  echo "str_contains() performs case-sensitive check.\n";
}
?>

The output of the above code will be:

Checking for empty string always returns true.
Hello contains 'll'.
str_contains() performs case-sensitive check.

❮ PHP String functions

5