PHP substr_compare() Function
Definition
The substr_compare() function compares two strings from a specified start position.
Syntax
PHP substr_compare() Function has the following syntax.
substr_compare(string1,string2,startpos,length,case)
Parameter
Parameter | Is Required | Description |
---|---|---|
string1 | Required. | First string to compare |
string2 | Required. | Second string to compare |
startpos | Required. | Where to start comparing. If negative, it starts counting from the end of the string |
length | Optional. | How long of string1 to compare |
case | Optional. | FALSE - Default. Case-sensitive. TRUE - Case-insensitive |
Return
This function returns:
- 0 - if the two strings are equal
- <0 - if string1 (from startpos) is less than string2
- >0 - if string1 (from startpos) is greater than string2
If length is equal or greater than length of string1, this function returns FALSE.
Example 1
<?php
echo substr_compare("Hello world","Hello world",0);
?>
The code above generates the following result.
Example 2
Compare two strings, when start position in string1 for the comparison is 6th:
<?php
echo substr_compare("Hello world","world",6);
?>
The code above generates the following result.