PHP soundex() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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

Words that sound alike usually get the same four-character key.

Syntax

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

PHP
$key = soundex($name);
CallWhat it does
soundex("Robert")Returns "R163".
soundex("Rupert")Also returns "R163".

A Soundex key is one letter followed by three digits, such as R163. It works best for English names.

Library declarations
PHP
soundex(string $string): string

Parameters

ParameterDescription
stringRequired. The word or name to convert.

Return Value

Return ValueDescription
stringA four-character Soundex key. An empty string input returns an empty string.

PHP Examples

Example 1 — Get a Soundex Key

Two names that sound alike share a key.

PHP
<?php
echo soundex("Robert"), PHP_EOL;
echo soundex("Rupert"), PHP_EOL;

How It Works

  1. Pass a name. The first letter stays as the prefix.
  2. Letters become digits. Similar sounds get the same digit.
  3. Pass a second name. It sounds close to the first.
  4. Compare keys. Both print R163.

Example 2 — Empty Strings and Padding

Short or empty input still follows the same rules.

PHP
<?php
var_dump(soundex(""));
echo soundex("Lee"), PHP_EOL;
echo soundex("Ashcraft"), PHP_EOL;

How It Works

  1. Pass an empty string. There is nothing to encode.
  2. Get an empty string. PHP 8 returns "".
  3. Short words are padded. Lee becomes L000.
  4. Letters like h are skipped. Ashcraft gives A261.

Example 3 — Match Names That Sound Alike

Compare two keys to see if names might be the same.

PHP
<?php
$a = "Smith";
$b = "Smyth";

echo soundex($a), PHP_EOL;
echo soundex($b), PHP_EOL;
echo soundex($a) === soundex($b) ? "Same sound" : "Different sound", PHP_EOL;

How It Works

  1. Pick two spellings. They are written differently.
  2. Get both keys. Each name goes through soundex().
  3. Compare with ===. Equal keys mean a likely sound match.
  4. Use it in search. This helps find names with spelling mistakes.

Frequently Asked Questions

soundex() turns a word into a four-character key that represents how it sounds in English. Words that sound alike usually share the same key.
soundex(string $string): string. Pass one word and you get back a key such as R163.
It is one letter followed by three digits. If there are not enough sounds, zeros are added at the end, like L000.
In PHP 8, it returns an empty string. Older PHP versions returned false.
Both compare sounds. soundex() gives a fixed four-character key. metaphone() returns a variable-length phonetic code that is often more accurate for English words.

Next: sprintf()

sprintf() builds a string from a format template and returns it.

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