PHP similar_text() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, similar_text() counts how many characters two strings have in common.

It can also store the similarity as a percentage in a third variable.

Syntax

Call similar_text() with the values you need. Start with this line.

PHP
$matches = similar_text($a, $b, $percent);
CallWhat it does
similar_text("World", "Word")Returns 4 matching characters.
similar_text($a, $b, $percent)Also sets $percent to a similarity score.

The order of the two strings can change the result. The percent is calculated as matches × 2 ÷ total length × 100.

Library declarations
PHP
similar_text(string $string1, string $string2, float &$percent = null): int

Parameters

ParameterDescription
string1Required. The first string.
string2Required. The second string.
percentOptional. A variable passed by reference. It receives the similarity as a percentage.

Return Value

Return ValueDescription
intThe number of matching characters between the two strings.

PHP Examples

Example 1 — Compare Two Similar Words

Get the number of matching characters and the percent.

PHP
<?php
$matches = similar_text("World", "Word", $percent);

echo "Matches: ", $matches, PHP_EOL;
echo "Percent: ", round($percent, 2), PHP_EOL;

How It Works

  1. Pass two strings. They are almost the same word.
  2. Add a variable. $percent is filled by reference.
  3. Read the count. Four characters match.
  4. Round the percent. round() keeps the output short.

Example 2 — Identical and Empty Strings

Equal strings score 100%. An empty string scores 0.

PHP
<?php
similar_text("PHP", "PHP", $same);
similar_text("", "abc", $empty);

echo "Same: ", $same, PHP_EOL;
echo "Empty: ", $empty, PHP_EOL;

How It Works

  1. Compare equal strings. Every character matches.
  2. Get 100. The percent is full.
  3. Compare with an empty string. Nothing can match.
  4. Get 0. No shared characters means no similarity.

Example 3 — Argument Order Matters

Swapping the strings can change the score.

PHP
<?php
$a = similar_text("bafoobar", "barfoo", $p1);
$b = similar_text("barfoo", "bafoobar", $p2);

echo $a, " (", round($p1, 2), "%)", PHP_EOL;
echo $b, " (", round($p2, 2), "%)", PHP_EOL;

How It Works

  1. Run it one way. The first call finds 5 matching characters.
  2. Swap the strings. The second call finds only 3.
  3. Compare the percents. The scores are different too.
  4. Remember the lesson. Always use the same order when you compare many values.

Frequently Asked Questions

similar_text() counts the characters two strings share, using an algorithm that finds the longest common part first. It returns that count and can also give a percentage.
similar_text(string $string1, string $string2, float &$percent = null): int. The third argument is optional and receives the percent by reference.
The percent is matches × 2 ÷ (length of string1 + length of string2) × 100. Two identical strings give 100.
Yes. similar_text("a", "b") may not match similar_text("b", "a") for longer strings. Pick one order and stay with it.
Yes. "PHP" and "php" do not match each other. Use strtolower() on both strings first if you want a case-insensitive check.

Next: soundex()

soundex() creates a Soundex key for a word based on how it sounds in English.

soundex() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful