How to Use the StartsWith() and EndsWith() Functions in PHP
Sometimes, the work with PHP requires to check whether a string starts or ends with a particular string. Learn how to do it with the help of our tutorial.
Sometimes, while working with PHP, it is necessary to test whether a string starts or ends with a particular character or substring. While PHP does not have native startsWith() or endsWith() functions, you can implement custom versions for older PHP releases or use the native functions introduced in PHP 8.0. On this page, we will show you how they work and how you can use them efficiently.
StartsWith()
The custom <kbd class="highlighted">startsWith()</kbd> function is generally applied for detecting whether a string starts with a particular substring or not. It returns a boolean value. The example below uses strict comparison, making it case-sensitive. You can use it within conditional statements or array filtering operations.
The syntax of <kbd class="highlighted">startsWith()</kbd> is the following:
php startswith syntax
bool startsWith( string, startString )Two parameters are accepted by this function: string and <kbd class="highlighted">startString</kbd>. The first parameter is applied for holding the text that needs to be tested. The second one is the text to look for at the beginning of the String. Once it’s an empty string, it will return true.
On success the <kbd class="highlighted">startsWith()</kbd> function returns true, otherwise, false. Here is an example of returning false:
php startswith function
<?php
// Function for checking the string starting
// with a particular substring
function startsWith($string, $startString)
{
$len = strlen($startString);
return substr($string, 0, $len) === $startString;
}
// Main function
if (startsWith("abcde", "c")) {
echo "True";
} else {
echo "False";
}
?>In the example below, true is returned:
php startswith function true
<?php
// Function for checking the string starting
// with a particular substring
function startsWith($string, $startString)
{
$len = strlen($startString);
return substr($string, 0, $len) === $startString;
}
// Main function
if (startsWith("abcde", "a")) {
echo "True";
} else {
echo "False";
}
?>EndsWith()
The custom <kbd class="highlighted">endsWith()</kbd> function is similar to the <kbd class="highlighted">startsWith()</kbd> function, but it is applied for testing whether a string ends with a certain string or not. It returns a boolean value. The example below uses strict comparison, making it case-sensitive. You can use it within conditional statements or array filtering operations.
The syntax of <kbd class="highlighted">endsWith()</kbd> looks like this:
php endswith function syntax
bool endsWith( string, endString )It includes the following two parameters: string and endString. The first parameter is applied for holding the text that needs to be tested. The second one is the text to look for at the end of the String. It can return true or false, as shown in the examples below:
php endswith function
<?php
// Function for checking the string ends
// with a particular substring or not
function endsWith($string, $endString)
{
$len = strlen($endString);
if ($len == 0) {
return true;
}
return substr($string, -$len) === $endString;
}
// Driver code
if (endsWith("abcde", "de")) {
echo "True";
} else {
echo "False";
}
?>php endswith function false
<?php
// Function for checking the string ends
// with a particular substring or not
function endsWith($string, $endString)
{
$len = strlen($endString);
if ($len == 0) {
return true;
}
return (substr($string, -$len) === $endString);
}
// Driver code
if (endsWith("abcde", "dgfe")) {
echo "True";
} else {
echo "False";
}
?>Native PHP 8.0+ Functions
Starting with PHP 8.0, the language includes native functions for these checks: str_starts_with() and str_ends_with(). They work similarly to the custom implementations above but are optimized for performance and do not require manual definition.
So, these functions are among the most commonly used ones in PHP. Learning how to use them will significantly enhance your success while working with PHP.