PHP 8 or newer:

Use the str_starts_with function:

str_starts_with('http://www.google.com', 'http')

PHP 7 or older:

Use the substr function to return a part of a string.

substr( $string_n, 0, 4 ) === "http"

If you're trying to make sure it's not another protocol. I'd use http:// instead, since https would also match, and other things such as http-protocol.com.

substr( $string_n, 0, 7 ) === "http://"

And in general:

substr($string, 0, strlen($query)) === $query
Answer from Kendall Hopkins on Stack Overflow
🌐
PHP
php.net › manual › en › function.str-starts-with.php
PHP: str_starts_with - Manual
Returns true if haystack begins with needle, false otherwise. ... <?php $string = 'The lazy fox jumped over the fence'; if (str_starts_with($string, 'The')) { echo "The string starts with 'The'\n"; } if (str_starts_with($string, 'the')) { echo ...
🌐
W3Schools
w3schools.com › php › func_string_str_starts_with.asp
PHP str_starts_with() Function
The str_starts_with() function checks if a string starts with a specific substring. Note: This function is case-sensitive. Note: This function is binary-safe. ... <?php $txt = "I really love PHP!"; var_dump(str_starts_with($txt, "i really")); ...
Discussions

str_starts_with but for arrays
Assuming you want a method that returns the valid options in your array (keys) based on some given input, you can use str_starts_with with any old function, or for a one liner, combine it with array filter: $options = array(1 => 'play_story', 10 => 'call_mom', 200 => 'call_pop', 3 => 'play_story'); $prefix = '1'; // option 1 function getValidOptions(array $allOptions, string $prefix) { $return = []; foreach ($allOptions as $number => $action) { if (str_starts_with((string) $number, $prefix)) { $return[$number] = $action; } } return $return; } $validOptions = getValidOptions($options, $prefix); var_dump($validOptions); // option 2 (one liner) $validOptions = array_filter($options, fn($el) => str_starts_with((string) $el, $prefix), ARRAY_FILTER_USE_KEY); var_dump($validOptions); More on reddit.com
🌐 r/PHPhelp
6
1
July 17, 2022
str_starts_with slower than userland
Description Firstly, apologies if this is the wrong place. I was reviewing some code going into a project, and it used str_starts_with. Being new to using it myself wondered about its performance c... More on github.com
🌐 github.com
21
May 1, 2025
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-str_starts_with-function
PHP str_starts_with() Function - GeeksforGeeks
August 4, 2021 - If the string $name will begin with the substring $beginsWith then str_starts_with() will return TRUE otherwise it will return FALSE and the value for $result will be assigned accordingly.
🌐
Codecademy
codecademy.com › docs › php › string functions › str_starts_with()
PHP | String Functions | str_starts_with() | Codecademy
June 28, 2023 - The str_starts_with() function returns true if the $str starts with the $substr or false otherwise.
🌐
OnlinePHP
onlinephp.io › str-starts-with › examples
str_starts_with - Examples
PHP Functions · Donate/Get Premium · Login / Register · OnlinePHP.io · Documentation · str_starts_with · Examples · PHP 8 · str_starts_with - Checks if a string starts with a given substring · Str Starts With Online Tool · Manual · ...
🌐
PHP
wiki.php.net › rfc › add_str_starts_with_and_ends_with_functions
PHP RFC: Add str_starts_with() and str_ends_with() functions
March 25, 2020 - Typically this functionality is accomplished by using existing string functions such as substr, strpos/strrpos, strncmp, or substr_compare (often combined with strlen). These bespoke userland implementations have various downsides, discussed further below. The str_starts_with and str_ends_with ...
🌐
GitHub
gist.github.com › juliyvchirkov › 8f325f9ac534fe736b504b93a1a8b2ce
php: polyfills of string functions str_starts_with, str_contains and str_ends_with · GitHub
It exits early if PHP 8.0 is detected. It's quite complex how these functions are called, it does not use strict types. First it uses ?string nullable types and then $haystack ?? '' to call the actual polyfill that has string types. Meaning that these functions can be called with null as arguments that get tuned into empty strings.
Find elsewhere
🌐
OnlinePHP
onlinephp.io › str-starts-with
str_starts_with - Online Tool
PHP Functions · String Manipulation · str_starts_with · Execute str_starts_with with this online tool str_starts_with() - Checks if a string starts with a given substring · Str Starts With Online Tool · Manual · Code Examples · Str Starts ...
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php str_starts_with
PHP str_starts_with(): Check If a String starts with a Substring
May 8, 2021 - It’s important to keep in mind that the str_starts_with() function performs a case-sensitive search. For example: <?php $str = 'PHP tutorial'; $substr = 'php'; $result = str_starts_with($str, $substr) ?
🌐
WordPress Trac
core.trac.wordpress.org › ticket › 58220
#58220 (Replace usage of substr with str_starts_with and str_ends_with) – WordPress Trac
WordPress currently supports down to PHP 5.6. got it, sorry. I just spotted that in the latest compat file. the version I noticed this fatal erroring on doesn't have the str_ functions in the compat file, so I just need to upgrade. According to the ​Hello Dolly plugin page, it should be compatible with WordPress 4.6 or higher. The str_starts_with function would require either PHP8 or WP5.9.
🌐
Reddit
reddit.com › r/phphelp › str_starts_with but for arrays
r/PHPhelp on Reddit: str_starts_with but for arrays
July 17, 2022 -

Hi,

I am building a phone system where I have a list of extensions that can be pressed. For example we can have an array like this:
$options = array(1 => 'play_story', 10 => 'call_mom', 200 => 'call_pop', 3 => 'play_story')
So if someone presses 1 it could be they are only pressing 1 or they could be pressing 10 so I need to wait to see if they press a 0 or not.. If someone presses 2 then the only option is for them to press 200 (though it could be they will try 201 which is invalid). As soon as I get 200 I know it's the only valid option. If they press 3 right away since I know it's the only option I don't have to wait for any more digits.

I need a way to be able to see if 1) What they entered is even valid. So say for instance if someone presses 4 or press 21 I know right away it's not valid since the array does not have any number that starts or is 4 or 21. I have found str_starts_with but that seems to match a specific string. Is there anything like str_starts_with that would check the array key or I would need to loop through each one, each time?

TIA.

Dovid

🌐
GitHub
github.com › php › php-src › issues › 18474
str_starts_with slower than userland · Issue #18474 · php/php-src
May 1, 2025 - generating tests........................................done! strncmp_startswith2: 49.7 ms strncmp_startswith: 88.3 ms substr_compare_startswith: 89.7 ms str_starts_with: 133.4 ms substr_startswith: 156.3 ms strpos_startswith: 201.2 ms preg_match_startswith: 15,547.4 ms · PHP 8.3 had similar results.
Author: php
🌐
Exakat
php-dictionary.readthedocs.io › en › latest › dictionary › str_starts_with.ini.html
str_starts_with() — PHP Dictionary 1.0.167 documentation
June 15, 2026 - str_starts_with() is a case-sensitive. Use strtolower() to remove case, and make case-insensitive comparisons. It has an complementary function called str_ends_with(). <?php var_dump(str_starts_with('abc', 'a')); // true var_dump(str_starts_with('abc', 'b')); // false var_dump(str_starts_w...
🌐
W3cubDocs
docs.w3cub.com › php › function.str-starts-with
Str_starts_with - PHP - W3cubDocs
Returns true if haystack begins with needle, false otherwise. ... <?php $string = 'The lazy fox jumped over the fence'; if (str_starts_with($string, 'The')) { echo "The string starts with 'The'\n"; } if (str_starts_with($string, 'the')) { echo 'The string starts with "the"'; } else { echo '"the" ...
🌐
Code.mu
code.mu › en › php › manual › string › str_starts_with
The str_starts_with Function - Checking the Start of a String in PHP
Let's check if the string starts with a given character: <?php $str = 'abcde'; $res = str_starts_with($str, 'a'); var_dump($res); ?>
🌐
learnhindituts
learnhindituts.com › practice › php-str-starts-with
PHP str_starts_with() | learnhindituts
str_starts_with() function check करता है कि क्या कोई String pass की गयी substring value से start हो रही है। match होने पर true return होता है otherwise false .