Seven Lesser‑Known PHP Built‑in Functions and How to Use Them
This article introduces seven relatively obscure but highly useful PHP built‑in functions—highlight_string, str_word_count, levenshtein, get_defined_vars, escapeshellcmd, checkdate, and php_strip_whitespace—explaining their purpose and providing clear code examples for each.
PHP provides a large number of built‑in functions, many of which are widely used, while some remain hidden in the corners of the language yet offer great utility; this article showcases seven such functions.
1. highlight_string() – Useful for displaying PHP code with syntax highlighting. It returns or outputs a highlighted version of the supplied code.
<?php
highlight_string('<?php phpinfo(); ?>');
?>2. str_word_count() – Returns the number of words in a string when passed a string argument.
<?php
$str = "How many words do I have?";
echo str_word_count($str); // Outputs 6
?>3. levenshtein() – Calculates the Levenshtein (edit) distance between two strings, useful for detecting misspellings.
<?php
$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); // Outputs 2
?>4. get_defined_vars() – Returns a multidimensional array containing all defined variables, including environment, server, and user‑defined variables.
print_r(get_defined_vars());
// Array ( [_GET] => Array ( ) [_POST] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) )5. escapeshellcmd() – Escapes special characters in a string to prevent command‑injection attacks; often used together with exec() or system().
<?php
$command = './configure ' . $_POST['configure_options'];
$escaped_command = escapeshellcmd($command);
system($escaped_command);
?>6. checkdate() – Validates a Gregorian date; returns true for valid dates and false otherwise.
<?php
var_dump(checkdate(12, 31, 2000)); // bool(true)
var_dump(checkdate(2, 29, 2001)); // bool(false)
?>7. php_strip_whitespace() – Returns the source code of a file with comments and whitespace removed, useful for comparing code size versus comments.
<?php
// PHP comment here
/*
* Another PHP comment
*/
echo php_strip_whitespace(__FILE__);
// Newlines are considered whitespace, and are removed too:
do_nothing();
?>These functions expand the toolbox of PHP developers, offering concise solutions for syntax highlighting, word counting, fuzzy string matching, variable inspection, command safety, date validation, and code size analysis.
Signed-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.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
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.
