PHP metaphone() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, metaphone() turns a word into a short code based on how it sounds in English.

Words that sound alike often get the same code. That helps with fuzzy name matching.

Syntax

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

PHP
$code = metaphone("Thompson");
CallWhat it does
metaphone("Thompson")Returns "TMSN".
metaphone("Thumb")Returns "0M". The 0 stands for the "th" sound.
metaphone("Thompson", 2)Returns "TM". The result is cut to 2 phonemes.

metaphone() works best for English words. The result uses uppercase letters and the digit 0.

Library declarations
PHP
metaphone(string $string, int $max_phonemes = 0): string

Parameters

ParameterDescription
stringRequired. The word to encode.
max_phonemesOptional. The maximum length of the code. Default is 0, which means no limit.

Return Value

Return ValueDescription
stringThe metaphone key of the word.

PHP Examples

Example 1 — Get the Metaphone Key

Pass a word and get its sound code.

PHP
<?php
echo metaphone("Thompson"), PHP_EOL;
echo metaphone("Thumb"), PHP_EOL;

How It Works

  1. Pass a name. Thompson is a good test word.
  2. Silent letters drop out. The P is not kept in the key.
  3. Th can become 0. Thumb starts with the zero sign.
  4. Read the output. Each call returns a short uppercase key.

Example 2 — Limit the Key Length

Use the second argument to cut the code short.

PHP
<?php
echo metaphone("Thompson", 2), PHP_EOL;
echo metaphone("Thompson", 0), PHP_EOL;

How It Works

  1. Pass a limit of 2. Only the first two phonemes are kept.
  2. Pass 0. Zero means no limit.
  3. Short keys match more. Shorter codes make looser comparisons.
  4. Choose with care. A limit helps with long names but can cause false matches.

Example 3 — Case Does Not Matter

The same word in different cases gives the same key, so you can compare keys directly.

PHP
<?php
$a = metaphone("THOMPSON");
$b = metaphone("thompson");

echo $a, PHP_EOL;
echo $b, PHP_EOL;
echo $a === $b ? "Same key" : "Different key", PHP_EOL;

How It Works

  1. Use two spellings. One is all caps, one is all lowercase.
  2. Encode both. metaphone ignores letter case.
  3. Compare the keys. They are equal.
  4. Use it for search. Compare keys to find names that sound alike.

Frequently Asked Questions

metaphone() creates a phonetic key for a word. Words that sound similar in English usually produce the same or a similar key.
string metaphone(string $string, int $max_phonemes = 0). The second argument limits the key length.
metaphone() is generally more accurate for English because it understands more pronunciation rules. soundex() always gives a 4-character code.
It is designed for English. For other languages the keys can be less reliable.
No. "THOMPSON" and "thompson" give the same key.

Next: money_format()

money_format() used to format a number as a currency string using the locale.

money_format() 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