wppaste
WordPress

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

Since
5.9.0
Source
wp-includes/compat.php:499

Checks whether one string appears anywhere inside another, returning a boolean instead of the mixed offset that strpos() returns. It exists as a compatibility shim for the native PHP 8.0 str_contains(), so plugin and theme code can call it safely on any PHP version WordPress supports. Pair it with str_starts_with() or str_ends_with() when the match has to be anchored rather than anywhere in the string.

Polyfill for str_contains() function added in PHP 8.0.

Description

Performs a case-sensitive check indicating if needle is contained in haystack.

Compatibility

WordPress
since 5.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$haystackstring
The string to search in.
$needlestring
The substring to search for in the $haystack.

Return value

bool
True if $needle is in $haystack, otherwise false.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Find posts whose title contains a keyword

Loop over the sample posts and print only the ones whose title contains a given word.

$keyword = 'world';
$matches = array();

for ( $post_id = 1; $post_id <= 5; $post_id++ ) {
	$title = get_the_title( $post_id );

	if ( '' !== $title && str_contains( $title, $keyword ) ) {
		$matches[] = $title;
	}
}

if ( $matches ) {
	echo 'Titles containing "' . esc_html( $keyword ) . '": ' . esc_html( implode( ', ', $matches ) );
} else {
	echo 'No titles contained "' . esc_html( $keyword ) . '".';
}

get_the_title() returns an empty string for a missing post ID, so that case is skipped rather than crashing.

Detect whether a stored price includes a decimal point

Read the price meta value already stored on post 2 and check its format before formatting it for display.

$price = get_post_meta( 2, 'price', true );

if ( '' === $price ) {
	echo 'Post 2 has no price meta set.';
} elseif ( str_contains( $price, '.' ) ) {
	echo 'Price "' . esc_html( $price ) . '" already includes a decimal value.';
} else {
	echo 'Price "' . esc_html( $price ) . '" is a whole number, appending .00.';
}

Common problems and fixes · 3

Why does str_contains() return true when I search for an empty string?

The source explicitly short-circuits before ever calling strpos(): if $needle is an empty string, the function returns true immediately, regardless of $haystack. This mirrors native PHP 8 behavior but trips people who expect false for an empty query.

Why doesn't str_contains() match when the case is different?

The check runs on strpos(), which is case-sensitive, so 'Hello' will not be found inside 'hello world'. There is no case-insensitive parameter.

Do I still need to call str_contains() on servers running PHP 8 or newer?

Yes, it's safe to call unconditionally. WordPress only defines this polyfill when the native function is missing, and its logic matches PHP 8's own implementation exactly, so the same code works whether the site runs PHP 7 or PHP 8.1.

Alternatives and related functions

str_starts_with
When the substring has to be at the very beginning of the string rather than anywhere inside it.
str_ends_with
When the substring has to be at the very end of the string rather than anywhere inside it.
strpos
When you need to know the numeric position of the match, not just whether it exists.
stripos
When the search needs to ignore case instead of matching exactly like str_contains() does.

Performance profile

How much work a call to str_contains() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5–7

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 8.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost str_contains() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5–7none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev85–71
8.585–71
8.485–713 fewer instructions than PHP 8.3
8.3115–101
8.2115–101
8.1115–101
7.4115–101

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 50

Show all 50

Source code

	function str_contains( $haystack, $needle ) {		if ( '' === $needle ) {			return true;		} 		return false !== strpos( $haystack, $needle );	}

Changelog

Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/compat.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.