PHP substr_compare() Function
In this chapter you will learn:
- Definition for PHP substr_compare() Function
- Syntax for PHP substr_compare() Function
- Parameter for PHP substr_compare() Function
- Return for PHP substr_compare() Function
- Example - Compare two strings
- Example - Compare two strings, when start position in string1 for the comparison is 6th
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.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP substr_count() Function
- Syntax for PHP substr_count() Function
- Parameter for PHP substr_count() Function
- Return for PHP substr_count() Function
- Example - Count the number of times "world" occurs in the string
- Example - Using all parameters
- Example - Overlapped substrings
- Example - If the start and length parameters exceeds the string length, substr_count will output a warning
Home » PHP Tutorial » PHP String Functions