SoFunction
Updated on 2025-04-10

Seven interesting PHP functions

PHP has many built-in functions, most of which are widely used by programmers. But there are also some functions hidden in the corner. This article will introduce to you 7 little-known but very useful functions. Programmers who have never used it may wish to come and have a look.

1.highlight_string()
When you need to display PHP code in a website, the highlight_string() function becomes very useful. This function uses the PHP syntax highlighting color defined in the program, outputs or returns a syntax highlighting version of the given PHP code.
Example:

Copy the codeThe code is as follows:
<?php
highlight_string('<?php phpinfo(); ?>');
?>

2.str_word_count()
This function must pass a parameter, which returns the number of words according to the parameter type. As shown below:

Copy the codeThe code is as follows:
<?php
$str = "How many words do I have?";
echo str_word_count($str); //Outputs 6
?>

()
This function mainly returns the Levenshtein distance between two strings. Levenshtein distance, also known as edit distance, refers to the minimum number of editing operations required to convert one into the other between two strings. The licensed editing operation includes replacing one character with another character, inserting one character, and deleting one character. This function is very useful for finding typos submitted by the user.

Example:

Copy the codeThe code is as follows:
<?php
$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); //Outputs 2
?>

4.get_defined_vars()
This function returns a multidimensional array containing a list of all defined variables, including environment variables, server variables, and user-defined variables.

Example:

Copy the codeThe code is as follows:
print_r(get_defined_vars());

()
This function is used to avoid special symbols in strings and can prevent users from using tricks to crack the server system. This function can be used with two functions: exec() or system(), which can reduce malicious corruption by online users.

Example:

Copy the codeThe code is as follows:
<?php
$command = './configure '.$_POST['configure_options'];
$escaped_command = escapeshellcmd($command);
system($escaped_command);
?>

()
This function can be used to check whether the date is valid, such as the year is 0 to 32767, the month is 1 to December, and the day changes with the month and leap year.

Example:

Copy the codeThe code is as follows:
<?php
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));
//Output
//bool(true)
//bool(false)
?>

7.php_strip_whitespace()
This function can return source code files with deleted PHP comments and whitespace characters, which is useful for comparisons between the actual number of code and the number of comments.

Example:

Copy the codeThe code is as follows:
<?php
// PHP comment here
/*
 * Another PHP comment
 */
echo php_strip_whitespace(__FILE__);
// Newlines are considered whitespace, and are removed too:
do_nothing();

//Try the output results
echo php_strip_whitespace(__FILE__); do_nothing();
?>