PHP str_starts_with() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, str_starts_with() checks whether a string begins with a given piece of text.

It returns true or false, is case-sensitive, and needs PHP 8.0 or newer.

Syntax

Call str_starts_with() with the values you need. Start with this line.

PHP
$ok = str_starts_with($url, "https://");
CallWhat it does
str_starts_with("PHP rocks", "PHP")Returns true because the string begins with "PHP".
str_starts_with("PHP rocks", "php")Returns false because the check is case-sensitive.
str_starts_with("PHP rocks", "")Returns true because an empty needle always matches.

Available since PHP 8.0. On older versions use strncmp() or substr() to get the same result.

Library declarations
PHP
str_starts_with(string $haystack, string $needle): bool

Parameters

ParameterDescription
haystackRequired. The string to search in.
needleRequired. The text the string should begin with.

Return Value

Return ValueDescription
booltrue if haystack starts with needle, otherwise false.

PHP Examples

Example 1 — Check a URL Prefix

The everyday use: does a string begin with some text?

PHP
<?php
$url = "https://codetofun.com";

echo str_starts_with($url, "https://") ? "Secure link" : "Not secure", PHP_EOL;
echo str_starts_with($url, "http://") ? "Plain link" : "Not a plain link", PHP_EOL;

How It Works

  1. Store the string. $url holds a full web address.
  2. Test the prefix. The first call checks for "https://".
  3. Get a boolean. The result is true, so the ternary prints "Secure link".
  4. Try another prefix. "http://" does not match, so the result is false.

Example 2 — Case and Empty Needle

Two behaviours to remember: matching is case-sensitive, and an empty needle always matches.

PHP
<?php
var_dump(str_starts_with("PHP", ""));
var_dump(str_starts_with("PHP", "ph"));
var_dump(str_starts_with("PHP", "PH"));

How It Works

  1. Empty needle. Every string starts with nothing, so the result is true.
  2. Different case. "ph" is not the same as "PH", so it returns false.
  3. Exact case. "PH" matches the first two letters.
  4. Tip. Lowercase both sides first if you need a case-insensitive check.

Example 3 — Filter a List of File Names

Loop over an array and keep only names that start with a prefix.

PHP
<?php
$files = ["img_01.png", "doc_01.pdf", "img_02.jpg", "notes.txt"];

foreach ($files as $file) {
    if (str_starts_with($file, "img_")) {
        echo $file, PHP_EOL;
    }
}

How It Works

  1. Create the list. Four file names, mixed types.
  2. Loop through them. foreach visits each name once.
  3. Check the prefix. Only names beginning with "img_" pass the if.
  4. Print matches. Two image names are printed, the rest are skipped.

Frequently Asked Questions

str_starts_with() checks if a string begins with a given substring. It returns true when it does and false when it does not.
bool str_starts_with(string $haystack, string $needle). The big string comes first and the prefix you are looking for comes second.
Yes. str_starts_with("Hello", "he") returns false. Convert both strings with strtolower() first if you want to ignore case.
PHP 8.0. On older versions, use substr($haystack, 0, strlen($needle)) === $needle or strncmp() instead.
It returns true. Since PHP 8.0 an empty string is treated as a prefix of every string.

Next: str_word_count()

str_word_count() counts the words in a string or returns them as an array.

str_word_count() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful