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

ParameterIs RequiredDescription
string1Required.First string to compare
string2Required.Second string to compare
startposRequired.Where to start comparing. If negative, it starts counting from the end of the string
lengthOptional.How long of string1 to compare
caseOptional.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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions