PHP str_contains()

clodaghw17's avatar
Published Jul 1, 2023
Contribute to Docs

The str_contains() function performs a case sensitive search for a given substring within a string. Checking for the presence of an empty string will always return a positive result.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours

Syntax

str_contains($aString, $aSubstring)
  • $aString: The string to be searched.
  • $aSubstring: The substring to be found.

Example

The following example demonstrates the case sensitive nature of str_contains().

<?php
$sentence = "I love New York";
$word = "new";
if(str_contains($sentence, $word)) {
echo $word." found.";
}
$word = "New";
if(str_contains($sentence, $word)) {
echo $word." found.";
}
?>

The above code results in the following output:

New found.

All contributors

Contribute to Docs

Learn PHP on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours