wppaste
WordPress

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

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

Determines whether a string begins with a given substring, using a case-sensitive strpos() comparison under the hood. WordPress loads this as a polyfill so the same str_starts_with() call works whether the site runs on PHP 7 or PHP 8, without any version checks in your code. An empty needle always returns true, matching PHP's own native function.

Polyfill for str_starts_with() function added in PHP 8.0.

Description

Performs a case-sensitive check indicating if the haystack begins with needle.

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 $haystack starts with $needle, 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.

Check if a post title starts with a given word

Fetch the first sample post and test its title against a prefix.

$post   = get_post( 1 );
$title  = $post->post_title;
$prefix = 'Hello';

if ( str_starts_with( $title, $prefix ) ) {
	printf( 'The title "%s" starts with "%s".', esc_html( $title ), esc_html( $prefix ) );
} else {
	printf( 'The title "%s" does not start with "%s".', esc_html( $title ), esc_html( $prefix ) );
}

The comparison is case-sensitive, so 'hello' would not match 'Hello world!'.

Filter category slugs by prefix

Loop over the site's categories and flag the ones whose slug starts with "new".

$categories = get_terms( array(
	'taxonomy'   => 'category',
	'hide_empty' => false,
) );

foreach ( $categories as $category ) {
	if ( str_starts_with( $category->slug, 'new' ) ) {
		printf( 'Category "%s" (slug: %s) matches the "new" prefix.<br>', esc_html( $category->name ), esc_html( $category->slug ) );
	} else {
		printf( 'Category "%s" (slug: %s) does not match.<br>', esc_html( $category->name ), esc_html( $category->slug ) );
	}
}

Common problems and fixes · 3

Why does str_starts_with return true when my needle is an empty string?

The function has an explicit early return for this case: if ( '' === $needle ) return true;. An empty needle is treated as matching the start of every string, which mirrors PHP's own native behavior rather than being a bug.

Why doesn't str_starts_with match when the casing is different?

The check runs through strpos(), which is case-sensitive, so 'Hello' and 'hello' are not considered a match.

Should I use str_starts_with or str_contains to check a prefix?

str_starts_with only returns true when the match sits at position 0 (strpos() === 0), so it only confirms the string begins with the needle. str_contains checks for the needle anywhere in the string, which is a different question.

Alternatives and related functions

str_ends_with
When you need to check the end of a string instead of the beginning, such as a file extension.
str_contains
When the needle can appear anywhere in the haystack, not only at the start.
strpos
When you also need the numeric position of a match, not just a true or false result.
substr
When you need to extract or compare a fixed-length slice of the string rather than test a prefix.

Performance profile

How much work a call to str_starts_with() 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_starts_with() 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_starts_with( $haystack, $needle ) {		if ( '' === $needle ) {			return true;		} 		return 0 === 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.