PHP 8 reference · String Functions

PHP str_starts_with() Function

Check whether a string begins with a given substring. Case-sensitive and returns true or false; added in PHP 8.0.

PHP 8 Since 8.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
haystack The string to check.
needle The prefix to look for.

Return value

true or false.

Examples

Check a prefixTry it yourself
<?php
$url = "https://readycamel.com";
var_dump(str_starts_with($url, "https://"));
var_dump(str_starts_with($url, "http://"));

Output

bool(true)
bool(false)
Filter linesTry it yourself
<?php
foreach (["# Title", "Some text", "# Another"] as $line) {
    if (str_starts_with($line, "# ")) {
        echo substr($line, 2), "\n";
    }
}

Output

Title
Another

Version history

VersionChange
PHP 8.0Added

In other languages

Was this page helpful?