Comparing Strings in PHP

The easiest way to compare PHP strings is to use the logical operators ('==') and ('!='). If you want to know which string is lexicographically greater or lesser, you can use the operators ('<'), ('>'), ('<='), ('>='). You can also compare strings using the strcmp($string1, $string2) and strcasecmp($string1, $string2) functions. The strcmp() function is a case-sensitive safe binary string comparison function that returns 0 if the strings match. The strcasecmp() function is similar but performs a case-insensitive comparison. In this PHP String Compare example, we compare strings using the ("==") operator. Click Execute to run the PHP Compare Strings Example online and see the result.
Comparing Strings in PHP Execute
<?php
$string1 = 'first string';
$string2 = 'second string';
 
if ($string1 == $string2) {
	echo 'Strings are equal';
}
else {
	echo 'Strings are not equal';
}
?>
  
Updated: Viewed: 8859 times

What is a string in PHP?

A PHP string is a sequence of characters where a character corresponds to a byte. PHP strings cannot support Unicode strings because a byte can only represent 256 characters. To create a string in PHP, you can use: single quotes ('...'), double quoted strings ("..."), and Heredoc syntax (<<<). PHP provides many built-in functions for replacing, comparing, interpolating, concatenating, and splitting strings. You can also convert string to int, convert string to lowercase, convert string to array, and find the length of the string.

PHP Comparing Strings Examples

The following are examples of string comparisons in PHP:

Comparing PHP strings using the logical operators

You can use the logical operators ("==") and ("!=") to compare two strings. You can use the ("==") operator to check if strings are equal and the ("!=") operator to check if strings are not equal.

Comparing two strings using "==" operator
<?php
$string1 = 'first string';
$string2 = 'second string';
 
if ($string1 == $string2) {
    echo 'Strings are equal';
}
else {
    echo 'Strings are not equal';
}
?>

#output: Strings are not equal

Comparing PHP strings using less than or greater than logical operators

You can compare ("<") or (">") comparisons between strings in PHP, as well as between numbers. If two strings start the same and then differ, then the string with the first character that differs will be smaller. This order is called lexicographic (the same is used in dictionaries).

PHP string comparison with '<' or '>' operators
<?php
$string1 = 'abc';
$string2 = 'abcd';
 
if ($string1 > $string2) {
    echo 'true';
}
else {
    echo 'false';
}
?>

#output: false

How to compare strings case insensitive in PHP?

To compare strings case-insensitively in PHP, you can use the strcasecmp() function. The function differs from strcmp() in that it does not compare the whole world but the first length bytes. If length is less than the length of the smallest of the strings, then the strings are compared as a whole. The strcasecmp() function compares two strings character-by-character (more precisely, byte-by-byte) and returns:

0: - if the strings match exactly;
-1: - if string str1 is lexicographically less than str2;
1: - if, on the contrary, str1 is "greater than" str2.

Since the comparison is byte-by-byte, the case of characters affects the results of comparisons.

PHP strcasecmp() Syntax
strcasecmp(str1, str2)

Where:
  • str1 and str2 (Required): specifies the first and second string to compare
PHP strcasecmp() Example
<?php
echo strcasecmp('Pineapple', 'pineapple');
?>

#output: 0

How то check if two strings are the same in PHP?

To compare two strings in PHP, you can use the strcmp() function. This function compares two strings character-by-character (more precisely, byte-by-byte) and returns:

0: - if the strings completely match;
-1: - if string str1 is lexicographically less than str2;
1: - if, on the contrary, str1 is greater than str2.

Since the comparison is byte-by-byte, the case of characters affects the results of comparisons.

PHP strcmp() Syntax
strcmp(str1, str2)

Where:
  • str1 and str2 (Required): specifies the first and second string to compare
PHP strcmp() Example
<?php
echo strcmp('I like PHP', 'I like PHP');
?>

#output: 0

See also