Categories: PHP

substr_compare – PHP String Functions

Syntax :

substr_compare ( string1, string2, start_position, length, case_insensitivity );

Description :

It’s an inbuilt function of PHP. substr_compare() function will compares two strings from a specified start position. It do the binary safe comparison of two strings.


Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description

string1, Required, String, First string to compare.

string2, Required, String, Second string to be compared.

start_position, Required, String,  Specifies where to start comparing in string1.. If value is negative then it starts counting from the end of the string.

length, Required, String, Specifies how much of string1 to compare.

case_insensitivity, Optional, Boolean , A boolean value that specifies whether or not to perform a case-sensitive compare. FALSEDefault. Case-sensitive. TRUECase-insensitive.

[/table]


Output :

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.


ChangeLog :

[table caption=”” width=”100%” colwidth=”50%|50%” colalign=”left|left”]
Version, Description
PHP 5.5.11 , length may now be 0..
PHP 5.1.0, Added the possibility to use a negative offset.
[/table]


Related articles :  strncmp().


substr_compare() – PHP Functions Example 1 :  if both string1 and string2 are same it returns 0.
<?php
echo substr_compare("Tutorialmines.net","Tutorialmines.net",0); 
?>

In above example, We have same strings to be compared. Output of above code in the browser is as below:

0

substr_compare() – PHP Functions Example 2 :  Compare two strings, when start position in string1 for the comparison is 6th:
<?php
echo substr_compare("Hello readers","readers",6);
?>

In above example ,We have a string1 “Hello readers” and string2 “readers”. This will compare string1 from 6th position. Output of above code in the browser is as below:

0

substr_compare() – PHP Functions Example 3 :  Using all possible parameters
<?php

// PHP program to show 
// substr_compare() function

echo substr_compare("tutor","or",3,2)."\n";

//start compare from the last of the string
echo substr_compare("tutor","or",-2,2)."\n";

//start compare from the last of the string
echo substr_compare("tutor","uto",1,2)."\n";

//Case-insensitive compare
echo substr_compare("tutor","UT",1,2,TRUE)."\n";

//Case-sensitive compare
echo substr_compare("tutor","UT",1,2)."\n";

echo substr_compare("tutor","ut",1,4)."\n";
echo substr_compare("tutor","ut",1,2)."\n";
?>

Output of above code is :

0
0
0
0
32
2
0


substr_compare() – PHP Functions Example 4 :  Different return values:
<?php
echo "\n".substr_compare("Hello readers!","Hello readers!",0); // the two strings are equal

echo "\n".substr_compare("Hello readers!","Hello",0); // string1 is greater than string2
echo "\n".substr_compare("Hello readers!","Hello readers here! ",0); // string1 is less than string2
 
?>

Output of above code is :

0
9
33

jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

6 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

6 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

6 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

6 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

7 months ago