PHP | startsWith() and endsWith() Functions

31 Jul, 2021
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:
<?php  // Function to check string starting// with given substringfunction startsWith ($string, $startString){    $len = strlen($startString);    return (substr($string, 0, $len) === $startString);}  // Main functionif(startsWith("abcde","c"))    echo "True";else    echo "False";?> 
False

Example 2:
<?php  // Function to check string starting// with given substringfunction startsWith ($string, $startString){    $len = strlen($startString);    return (substr($string, 0, $len) === $startString);}  // Main functionif(startsWith("abcde","a"))    echo "True";else    echo "False";?> 
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:
<?php  // Function to check the string is ends // with given substring or notfunction endsWith($string, $endString){    $len = strlen($endString);    if ($len == 0) {        return true;    }    return (substr($string, -$len) === $endString);}  // Driver codeif(endsWith("abcde","de"))    echo "True";else    echo "False";?> 
True

Example 2:
<?php  // Function to check the string is ends // with given substring or notfunction endsWith($string, $endString){    $len = strlen($endString);    if ($len == 0) {        return true;    }    return (substr($string, -$len) === $endString);}  // Driver codeif(endsWith("abcde","dgfe"))    echo "True";else    echo "False";?> 
False

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
snarfblam
PHP-function
Picked
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
PHP | Converting string to Date and DateTime
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?