How to Use the StartsWith() and EndsWith() Functions in PHP

Sometimes, while working with PHP, it is necessary to test whether a string starts/ends with a particular character/string or not. For that purpose, the startsWith() and endsWith() functions are used. On this page, we will show you how they work and how you can use them efficiently.

Watch a course Learn object oriented PHP

StartsWith()

The startsWith() function is generally applied for detecting whether a string starts at a particular string or not. It is a case-insensitive function and is capable of returning a boolean value. To look for data, you can use it along with the filter function.

The syntax of startsWith() is the following:

bool startsWith( string, startString )

Two parameters are accepted by this function: string and startString. 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 startsWith() function returns true, otherwise, false. Here is an example of returning false:

<?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

// 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 endsWith() function is similar to the startsWith() function, but it is applied for testing whether a string ends with a certain string or not. This function is also case insensitive, returning a boolean value. You can use it along with the Filter function for searching data.

The syntax of endsWith() looks like this:

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
// 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
  
// 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";
?>

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.