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
Syntax
str_starts_with(string $haystack, string $needle): bool
Parameters
| Parameter | Description |
|---|---|
haystack |
The string to check. |
needle |
The prefix to look for. |
Return value
true or false.
Examples
<?php
$url = "https://readycamel.com";
var_dump(str_starts_with($url, "https://"));
var_dump(str_starts_with($url, "http://"));
Output
bool(true) bool(false)
<?php
foreach (["# Title", "Some text", "# Another"] as $line) {
if (str_starts_with($line, "# ")) {
echo substr($line, 2), "\n";
}
}
Output
Title Another
Version history
| Version | Change |
|---|---|
| PHP 8.0 | Added |
In other languages
str.startswith()· Pythonstring.startsWith()· JavaScript
Related
str_ends_with()· Check whether a string ends with a given substring, such as a file extension. Case-sensitive; added in PHP 8.0.str_contains()· Check whether a string contains a substring and return true or false. Case-sensitive; added in PHP 8.0.substr()· Return part of a string, given a start position and an optional length. Negative values count from the end.- PHP Strings · tutorial