PHP | substr_compare() Function
The substr_compare() function is a built-in function in PHP and it helps to compare two strings from a specified start position upto a specified length.
Syntax:
int substr_compare($str1, $str2, $startpos, $len, $caseInsensitive)
Parameters: This function accepts a total of five parameters out of which first three are mandatory to be supplied and the rest two are optional. All of this parameters are described below:
- $str1(mandatory): This parameter represents the first string to compare.
- $str2(mandatory): This parameter represents the second string to compare.
- $startpos(mandatory): This parameter specifies where to start comparing in $str1. If startpos is negative then it starts comparing from the end of the string.
- $len(optional): This parameter specifies how much of $str1 to compare.
- $caseInsensitive(optional): This parameter represents a boolean value that specifies whether or not to perform a case-sensitive comparison. If it is set to FALSE then comparison will be Case-sensitive, If it is set to TRUE then comparison will be Case-insensitive
Return Value: This function returns an integer value based on the below cases:
- Returns a value less than 0 if $str1 starting from position $startpos is less than str2.
- Returns a value greater than 0 if $str1 starting from position $startpos greater than string2.
- Returns 0 if $str1 and $str2 are equal.
- If $startpos is equal to or greater than the length of $str1, or the length $len is set and is less than 1 then substr_compare() function prints a warning and returns FALSE.
Below program illustrate the substr_compare() Function in PHP:
<?php // PHP program to illustrate the // substr_compare() function echo substr_compare("geeks", "gfg", 2)."\n"; echo substr_compare("geeksforgeeks", "gfg", 2)."\n"; echo substr_compare("Geeks", "gfg", 0, 1, true)."\n"; echo substr_compare("Geeks", "gfg", 0, 3, true)."\n"; echo substr_compare("GeeksforGeeks", "geeksforgeeks", 0, false)."\n"; ?> |
Output:
-2 -2 0 -1 0
Reference:
http://php.net/manual/en/function.substr-compare.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- PHP | Ds\Set contains() Function
- PHP | tan( ) Function
- p5.js | nf() Function
- p5.js | arc() Function
- PHP | exp() Function
- CSS | var() Function
- p5.js | red() function
- PHP | Ds\Set last() Function
- p5.js | min() function
- D3.js | d3.set.add() Function
- PHP | dir() Function
- CSS | hsl() Function
- p5.js | hue() function
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.



