PHP substr_compare() Function
Example
Compare two strings:
Run Example »
Definition and Usage
The substr_compare() function compares two strings from a specified starting position.
**Tip:** This function is binary-safe and selectively case-sensitive.
Syntax
substr_compare(
string1,string2,startpos,length,case
)
| Parameters | Description |
|---|---|
| string1 | Required. Specifies the first string to compare. |
| string2 | Required. Specifies the second string to compare. |
| startpos | Required. Specifies where in string1 to start the comparison. If negative, counting starts from the end of the string. |
| length | Optional. Specifies the number of characters of string1 to compare. |
| case | Optional. A boolean specifying whether to perform a case-sensitive comparison:
|
Technical Details
| Return value: | This function returns:
- 0 - If the two strings are equal
- <0 - If string1 (from start position startpos) is less than string2
- >0 - If string1 (from start position startpos) is greater than string2
More examples
Example 1
Compare two strings, with the start position used for comparison in string1 set to 6:
Run Example »
Example 2
Using all parameters:
Run Example »
Example 3
Different return values:
Run Example »
PHP String Reference Manual