Unlock Powerful PHP Built‑in Functions: Levenshtein, Easter Date, Forks & More
Explore a collection of essential PHP built‑in functions—from string distance with levenshtein and phonetic matching via metaphone, to date calculations, DNS queries, array merging, email sending, dynamic extension loading, file globbing, and process forking—complete with clear code examples and expected outputs.
1. Levenshtein
The levenshtein() function calculates the edit distance between two strings, i.e., the minimum number of insertions, deletions, or substitutions required to transform one string into another.
Example returning zero distance for identical strings:
levenshtein("PHP is awesome", "PHP is awesome"); // 0When the strings differ, a larger distance is returned:
levenshtein("Dark colour schemes", "are awesome"); // 13Levenshtein can also be used to compute a similarity percentage:
$s1 = 'Tinywan';
$s2 = 'ShaoBoWan';
$max_length = max(strlen($s1), strlen($s2));
$distance = levenshtein($s1, $s2);
$similarity_percent = (1 - $distance / $max_length) * 100;
echo $similarity_percent . '%';This script outputs 22%, indicating a 22 % similarity between the two strings.
2. easter_date
The easter_date() function returns the Unix timestamp of Easter for a given year; if no year is supplied, the current year is used.
echo date('Y-m-d', easter_date(2023)); // 2023-04-083. forks (pcntl_fork)
When PHP is run via the CLI, the pcntl_fork() function (exposed through the pcntl extension) can create child processes, enabling concurrent execution.
Simple example of creating a child process with sockets:
function async(Process $process): Process {
socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets);
[$parentSocket, $childSocket] = $sockets;
if (($pid = pcntl_fork()) == 0) {
socket_close($childSocket);
socket_write($parentSocket, serialize($process->execute()));
socket_close($parentSocket);
exit;
}
socket_close($parentSocket);
return $process
->setStartTime(time())
->setPid($pid)
->setSocket($childSocket);
}4. metaphone
The metaphone() function generates a phonetic key for a string, allowing words that sound alike to be compared.
metaphone("Light color schemes!"); // LFTKLRSXMS
metaphone("Light colour schemes!"); // LFTKLRSXMS5. dns_get_record
dns_get_record()retrieves DNS records for a domain, returning arrays that may include A, MX, NS, SOA, TXT, etc.
dns_get_record("ai.tinywan.com");Sample output (truncated):
array(1) {
[0]=>
array(6) {
["host"]=> string(14) "ai.tinywan.com"
["class"]=> string(2) "IN"
["ttl"]=> int(3600)
["type"]=> string(3) "TXT"
["txt"]=> string(7) "RFC8482"
["entries"]=> array(2) {
[0]=> string(7) "RFC8482"
[1]=> string(0) ""
}
}
}6. array_merge_recursive
This function merges one or more arrays recursively, combining values with the same keys into sub‑arrays.
Basic usage
$array1 = ['a' => ['b', 'c']];
$array2 = ['a' => ['d', 'e']];
$result = array_merge_recursive($array1, $array2);
print_r($result);Result:
Array (
[a] => Array (
[0] => b
[1] => c
[2] => d
[3] => e
)
)Handling of string keys
When the arrays share the same string key, their values are merged into a single array under that key.
$array1 = ['a' => 'apple', 'b' => 'banana'];
$array2 = ['a' => 'pear', 'c' => 'cherry'];
$result = array_merge_recursive($array1, $array2);
print_r($result);Result:
Array (
[a] => Array (
[0] => apple
[1] => pear
)
[b] => banana
[c] => cherry
)7. mail
The mail() function sends an email directly from a PHP script, returning TRUE on success.
$txt = "First line of text
Second line of text";
$txt = wordwrap($txt, 70);
mail("[email protected]", "开源技术小栈", $txt);8. dl
The dl() function loads a PHP extension at runtime (available only in CLI builds).
if (!extension_loaded('sqlite')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
dl('php_sqlite.dll');
} else {
dl('sqlite.so');
}
}9. glob
glob()returns an array of file or directory names matching a pattern.
print_r(glob(__DIR__ . "/*.md"));Sample output:
Array (
[0] => D:\work\docs\dev\other/account.md
[1] => D:\work\docs\dev\other/changelog.md
[2] => D:\work\docs\dev\other/meet.md
[3] => D:\work\docs\dev\other/question.md
[4] => D:\work\docs\dev\other/wechat.md
)10. date_sun_info
date_sun_info()returns an array with sunrise, sunset, and various twilight times for a given timestamp and geographic coordinates.
$sun_info = date_sun_info(strtotime("2024-01-01"), 31.7667, 35.2333);
foreach ($sun_info as $key => $val) {
echo "$key: " . date("H:i:s", $val) . PHP_EOL;
}Typical output:
sunrise: 04:38:51
sunset: 14:45:50
transit: 09:42:21
civil_twilight_begin: 04:12:07
civil_twilight_end: 15:12:34
nautical_twilight_begin: 03:41:45
nautical_twilight_end: 15:42:56
astronomical_twilight_begin: 03:12:03
astronomical_twilight_end: 16:12:38Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
