PHP | startsWith() and endsWith() Functions

startsWith() Function

The StartsWith() function is used to test whether a string begins with the given string or not. This function is case insensitive and it returns boolean value. This function can be used with Filter function to search the data.
Syntax

bool startsWith( string, startString )

Parameters: This function accepts two parameters as mentioned above and described below:



  • string: This parameter is used to hold the text which need to test.
  • startString: The text to search at the beginning of String. If it is an empty string, then it returns true.

Return Value: This function returns True on success or False on failure.
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Function to check string starting
// with given 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";
?> 

chevron_right


Output:

False

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Function to check string starting
// with given 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";
?> 

chevron_right


Output:

True

endsWith() Function

The endsWith() function is used to test whether a string ends with the given string or not. This function is case insensitive and it returns boolean value. The endsWith() function can be used with the Filter function to search the data.

Syntax:

bool endsWith( string, endString )

Parameter:

  • string: This parameter holds the text which need to test.
  • endString: The text to search at the end of given String. If is an empty string, it returns true.

Return Value: This function returns True on success or False on failure.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Function to check the string is ends 
// with given 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";
?> 

chevron_right


Output:

True

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Function to check the string is ends 
// with given 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";
?> 

chevron_right


Output:

False


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : snarfblam



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.