Compare two strings in PHP

Description

The following code shows how to compare two strings.

Example


//  w  w w  .ja  v a2s  . c  om
<?php
    echo substr_compare("Hello world","Hello world",0);
    //Compare two strings, when start position in string1 for the comparison is 6th
    echo substr_compare("Hello world","world",6);
    //Using 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);


   //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
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone