Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
communitycreator

What is str_contains in PHP?

Dario Vincenzo Tarantini

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The str_contains function in PHP is used to check if a string is contained in another string.

Note: This function is only available in PHP 8 onwards.

Syntax

str_contains(string $haystack, string $needle): bool

Parameters

  • haystack: the string where we want to search
  • needle: the string we will search for

Return value

The function returns true if it finds the substring. Otherwise, it returns false.

Code

In this example, we will search for lo in Hello:

<?php
var_dump(str_contains('Hello', 'lo'));
?>

Expected output

bool(true)

RELATED TAGS

php
communitycreator

CONTRIBUTOR

Dario Vincenzo Tarantini

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring