PHP substr_compare() Function

PHP substr() Function
PHP substr_count() Function

what is the syntax of the UBSTR_COMPARE() function in php?

string1Required. Specifies the first string to compare
string2Required. Specifies the second string to compare
startposRequired. Specifies where to start comparing in string1. If negative, it starts counting from the end of the string
lengthOptional. Specifies how much of string1 to compare
caseOptional. A boolean value that specifies whether or not to perform a case-sensitive compare:FALSE – Default. Case-sensitiveTRUE – Case-insensitive

examples of the UBSTR_COMPARE() function

Example 1. In this example, we compare two strings.

echo substr_compare("Hello world","Hello world",0);

Example 2. In this example, we compare two strings, when start position in string1 for the comparison is 6th.

echo substr_compare("Hello world","world",6);

Example 3. In this example, we use all parameters.

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);

Example 4. In this example, we different return values.

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
PHP substr() Function
PHP substr_count() Function

Add comment

Tutor Network

Share this

Learn PHP from A to Z