
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.
[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. FALSE – Default. Case-sensitive. TRUE – Case-insensitive.
[/table]
This function returns:
If length is equal or greater than length of string1, this function returns FALSE.
[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]
<?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:
<?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:
<?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
<?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
When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…
In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…
In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…
In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…
When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…
In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…