Skip to content

PHP Tutorial Strings

Comparing Strings

Lesson 71 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

There are several ways to compare strings, depending on what you want to know.

Are they exactly the same? ===

index.php
PHP
<?php
var_dump("php" === "php");
var_dump("php" === "PHP");
Output
OUTPUT
bool(true)
bool(false)

Ignoring case: strcasecmp()

strcasecmp() returns 0 when the strings are equal, ignoring case.

index.php
PHP
<?php
var_dump(strcasecmp("php", "PHP") === 0);
Output
OUTPUT
bool(true)

Which comes first: strcmp()

strcmp() returns a negative number if the first string is smaller, 0 if they are equal, and a positive number if it is bigger. The exact size of the number may differ, so compare with < 0, === 0 or > 0.

index.php
PHP
<?php
var_dump(strcmp("apple", "banana") < 0);
var_dump(strcmp("same", "same") === 0);
var_dump(strcmp("b", "a") > 0);
Output
OUTPUT
bool(true)
bool(true)
bool(true)

Natural sorting: strnatcmp()

Computers sort "img10" before "img2", because 1 comes before 2. Humans expect img2 first. strnatcmp() sorts the way people do.

index.php
PHP
<?php
$files = ["img10.png", "img2.png", "img1.png"];

natsort($files);

echo implode(", ", $files);
Output
OUTPUT
img1.png, img2.png, img10.png

How similar are they?

index.php
PHP
<?php
similar_text("World", "Word", $percent);

echo round($percent, 1) . "%\n";
echo levenshtein("kitten", "sitting");
Output
OUTPUT
88.9%
3

levenshtein() counts how many single-character edits turn one string into the other. It is useful for “did you mean …?” suggestions.

Comparing secrets: hash_equals()

When you compare a token, a password hash or any secret, do not use ===. A normal comparison stops at the first different character. An attacker can measure tiny time differences to guess the secret one character at a time. This is called a timing attack.

hash_equals() takes the same time every time.

index.php
PHP
<?php
$known = "a1b2c3d4";
$sent = "a1b2c3d4";

var_dump(hash_equals($known, $sent));
Output
OUTPUT
bool(true)

The known value must come first and the user-supplied value second.

lightbulb
Remember the rule
For normal text, ===. For case-insensitive, strcasecmp(). For secrets and tokens, hash_equals().