Easy Tutorial
❮ Pdostatement Fetch Func Filter Has Var ❯

PHP substr_compare() Function

PHP String Reference Manual

Example

Compare two strings:


Definition and Usage

The substr_compare() function compares two strings from a specified start position.

Tip: This function is binary-safe and optionally case-sensitive.


Syntax

Parameter Description
string1 Required. Specifies the first string to compare.
string2 Required. Specifies the second string to compare.
startpos Required. Specifies where to start the comparison in string1. If negative, it counts from the end of the string.
length Optional. Specifies the number of characters to compare in string1.
case Optional. A boolean value that specifies whether to perform a case-sensitive comparison: FALSE - Default. Case-sensitive<br> TRUE - Case-insensitive

Technical Details

Return Value: The function returns: 0 - if the two strings are equal<br> <0 - if string1 (from start position startpos) is less than string2<br> >0 - if string1 (from start position startpos) is greater than string2. If length is greater than or equal to the length of string1, the function returns FALSE.
PHP Version: 5+
--- ---
Changelog: Negative startpos is allowed since PHP 5.1.
--- ---

More Examples

Example 1

Compare two strings with the start position for comparison in string1 being 6:

Example 2

Using all parameters:

Example 3

Different return values:


❮ Pdostatement Fetch Func Filter Has Var ❯