Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. strcmp() / strcasecmp()

strcmp() / strcasecmp() Since: PHP 4(2000)

Compares two strings and determines whether they are equal or which one comes first. Also supports case-insensitive comparison and natural order comparison.

Syntax

// Compares two strings in a binary-safe manner.
strcmp($string1, $string2);

// Compares two strings, ignoring case differences.
strcasecmp($string1, $string2);

// Compares two strings using a natural order algorithm.
strnatcmp($string1, $string2);

// Calculates the similarity between two strings.
similar_text($string1, $string2, &$percent);

Functions

FunctionDescription
strcmp($string1, $string2)Compares two strings in a binary-safe manner. Returns a negative value if $string1 is less than $string2, 0 if they are equal, or a positive value if $string1 is greater.
strcasecmp($string1, $string2)Compares two strings without regard to case. The return value follows the same rules as strcmp().
strnatcmp($string1, $string2)Compares two strings using a natural order algorithm. Numeric portions are interpreted as numbers, resulting in an ordering that feels natural to humans.
similar_text($string1, $string2, &$percent)Returns the number of matching characters between two strings. If a variable is passed as the third argument, the similarity is stored as a percentage.

Return Value

strcmp(), strcasecmp(), and strnatcmp() return a negative value if $string1 is less than $string2, 0 if they are equal, or a positive value if $string1 is greater. similar_text() returns the number of matching characters as an integer.

Sample Code

<?php
// Compare strings with strcmp().
echo strcmp("abc", "abc"); // Outputs 0 because they are equal.
echo strcmp("abc", "def"); // Outputs a negative value because "abc" is less than "def".
echo strcmp("def", "abc"); // Outputs a positive value because "def" is greater than "abc".

// strcasecmp() ignores case differences.
echo strcasecmp("Hello", "hello"); // Outputs 0 because they are considered equal.
echo strcasecmp("PHP", "php"); // Outputs 0 because they are considered equal.

// strnatcmp() treats numeric parts as numbers.
echo strcmp("file2", "file10"); // Outputs a positive value because "file2" comes after "file10" in lexicographic order.
echo strnatcmp("file2", "file10"); // Outputs a negative value because "file2" comes before "file10" in natural order.

// Practical example of natural order sorting.
$files = ["file10.txt", "file2.txt", "file1.txt", "file20.txt"];
usort($files, "strnatcmp");
var_dump($files); // Outputs array("file1.txt", "file2.txt", "file10.txt", "file20.txt") in natural order.

// Calculate similarity with similar_text().
$percent = 0;
$matched = similar_text("programming", "program", $percent);
echo $matched; // Outputs the number of matching characters.
echo round($percent, 1) . "%"; // Outputs the similarity as a percentage.

// Use === for password matching.
$input = "MyPassword123";
$stored = "MyPassword123";
if ($input === $stored) {
	echo "Password matched."; // Safe comparison using strict equality.
}

Overview

strcmp() compares two strings byte by byte. It returns 0 if they are equal, a negative value if $string1 comes before $string2 in dictionary order, or a positive value if it comes after. Use strcasecmp() for case-insensitive comparison.

strnatcmp() uses a natural order algorithm that interprets numeric portions as actual numbers. In standard lexicographic order, "file10" would come before "file2", but in natural order they are correctly sorted as "file2" then "file10". This is useful for sorting filenames and version numbers.

similar_text() calculates how similar two strings are, making it useful for fuzzy search and input suggestions. To search within a string, use strpos(). To convert letter case, use strtolower() / strtoupper().

If you find any errors or copyright issues, please .