PHP substr_compare() Function

## PHP Basic

PHP HOME PHP Intro PHP Install PHP Syntax PHP Variables PHP Echo / Print PHP Data Types PHP String Functions PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP While Loops PHP For Loops PHP Functions PHP Arrays PHP Sorting Arrays PHP Superglobals

## PHP Forms

PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

## PHP Advanced

PHP Arrays Multi PHP Date PHP Include PHP File PHP File Upload PHP Cookies PHP Sessions PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filter

## PHP Database

PHP MySQL Intro PHP MySQL Connect PHP CREATE DB/Table PHP INSERT INTO PHP SELECT PHP WHERE PHP ORDER BY PHP UPDATE PHP DELETE PHP ODBC

## PHP XML

XML Expat Parser XML DOM XML SimpleXML

## PHP and AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX RSS Reader AJAX Poll

## PHP Reference

PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones

## PHP Quiz

PHP Quiz PHP Certificate

# PHP substr_compare() Function

PHP String Reference

## Example

Compare two strings:

<?php
echo substr_compare("Hello world","Hello world",0);
?>
Run example »

## 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

substr_compare(string1,string2,startpos,length,case)

Parameter | Description
--- | ---
string1 | Required. Specifies the first string to compare
string2 | Required. Specifies the second string to compare
startpos | Required. Specifies where to start comparing in string1. If negative, it starts counting from the end of the string
length | Optional. Specifies how much of string1 to compare

case

Optional. A boolean value that specifies whether or not to perform a case-sensitive compare:

- FALSE - Default. Case-sensitive
- TRUE - Case-insensitive

## Technical Details

Return Value:

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.

PHP Version: | 5+
Changelog: | As of PHP 5.1, it is now possible to use a negative startpos

## More Examples

## Example 1

Compare two strings, when start position in string1 for the comparison is 6th:

<?php
echo substr_compare("Hello world","world",6);
?>
Run example »

## Example 2

Using all parameters:

<?php
echo substr_compare("world","or",1,2);
echo substr_compare("world","ld",-2,2);
echo substr_compare("world","orl",1,2);
echo substr_compare("world","OR",1,2,TRUE);
echo substr_compare("world","or",1,3);
echo substr_compare("world","rl",1,2);
?>
Run example »

## Example 3

Different return values:

<?php
echo substr_compare("Hello world!","Hello world!",0); // the two strings are equal
echo substr_compare("Hello world!","Hello",0); // string1 is greater than string2
echo substr_compare("Hello world!","Hello world! Hello!",0); // str1 is less than str2
?>
Run example »

PHP String Reference

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE