Zend PHP 7 Certification – Strings – Matching

This post covers the Matching section of the Strings chapter when studying for the Zend PHP 7 Certification.

There are a variety of functions in PHP we can use to compare and match two strings. Equal comparison operators used to compare if two strings are equal.

$string1 = 'string';
$string2 = 'something else';

var_dump($string1 == $string2); // Outputs: boolean false;

$variable1 = 45;
$variable2 = '45';

var_dump($variable1 == $variable2);  // Outputs: boolean true
var_dump($variable1 === $variable2); // Outputs: boolean false

strcmp() is a case-sensitive function that returns less than 0 if $var1 is less than $var2, greater than 0 if $var1 is greater than $var2, and 0 if they are equal.

$var1 = "Hello";
$var2 = "Hello";

echo strcmp($var1, $var2); // Outputs: 0

$var3 = "hello";

echo strcmp($var1, $var3); // Outputs: -32

Note that the output is not -1 or 1 if the left argument is less or more than the right argument. The numbers generated are a result of subtracting the ASCII value of the first character that does not match of both arguments via the ord() function.

The example above would output the following.

ord('H'); // 72
ord('h'); // 104

72 - 104 = -32

Also note that the function is type insensitive, so the following outputs 0.

$var1 = '123';
$var2 = 123;

echo strcmp($var1, $var2); // Outputs: 0

In PHP 7, you could also use the spaceship operator to compare two expressions that return -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b.

echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1

The strcasecmp() function is the case insensitive version of strcmp(), so the following would output 0 unlike the same example above using strcmp().

$var1 = 'Hello';
$var2 = 'hello';

echo strcmp($var1, $var2); // 0

The similar_text() function calculates the similarity between two strings. By passing in two parameters, similar_text() returns the number of characters that are found in each argument.

$var_1 = 'PHP IS GREAT'; 
$var_2 = 'WITH MYSQL'; 

echo similar_text($var_1, $var_2); // Outputs: 3

Note that you can also pass in a third parameter, $percent, that will calculate the similarity in percent for you.

$var_1 = 'PHP IS GREAT'; 
$var_2 = 'WITH MYSQL'; 

similar_text($var_1, $var_2, $percent); 

echo $percent; // Outputs: 27.272727272727

Be aware that using similar_text() on two empty strings will return 0. Another thing to be aware of is when using this function, that the order of passing the strings is very important if you want to calculate the percentage of similarity, in fact, altering the variables will give a very different result.

$var_1 = 'PHP IS GREAT'; 
$var_2 = 'WITH MYSQL'; 

similar_text($var_1, $var_2, $percent); 

echo $percent; 
// 27.272727272727 

similar_text($var_2, $var_1, $percent); 

echo $percent; 
// 18.181818181818 

The levenshtein() function calculates the Levenshtein distance between two strings.

The Levenshtein distance is defined as the minimal number of characters you have to replace, insert or delete to transform str1 into str2.

In its simplest form the function will take only the two strings as parameter and will calculate just the number of insert, replace and delete operations needed to transform str1 into str2.

$str1 = 'carrrot';
$str2 = 'carrot';

echo levenshtein($str1, $str2); // Outputs: 1

Additional parameters include:

  • cost_ins – Defines the cost of insertion.
  • cost_rep – Defines the cost of replacement.
  • cost_del – Defines the cost of deletion.

The below example defined the insert, replace and delete cost for $string1 to be converted to $string2.

$string1 = "Hello World"
$string2 = "ello World"
echo levenshtein($string1, $string2, 10, 20, 30); // Outputs: 30

This is because we have to delete the “H” from $string1 for the string to be converted into $string2.

View the other sections:

Note: This article is based on PHP version 7.1.