Home > Article > Backend Development > 7 little-known but very useful PHP functions_PHP Tutorial
This article mainly introduces 7 little-known but very practical PHP functions. This article summarizes such as highlight_string( ), str_word_count(), levenshtein(), get_defined_vars(), escapeshellcmd() and other very useful but uncommon functions. Friends who need them can refer to them
Overview
PHP has numerous built-in functions, most of which are widely used by developers. But there are also some that are equally useful but forgotten in the corner. This article will introduce 7 little-known but very cool functions.
Highlight_string()
When PHP code needs to be displayed on a web page, the highlight_string() function is very useful. This function returns a highlighted version of the code in the function using the colors defined built-in to PHP.
The code is as follows:
Highlight_string('');
echo highlight_string('',true);
?>
str_word_count()
This function can conveniently return the number of words in the input string parameter.
The code is as follows:
$str = "hello world";
echo str_word_count($str); //Output 2
?>
levenshtein()
This function can conveniently return the levenshtein (edit distance) between two parameters. I once encountered a requirement. When users edit their ID cards, they are limited to modifying only 4 digits. This function is used.
The code is as follows:
$idcard='230406198506206797';
$newIdcard='230406198506207798';
echo levenshtein($idcard,$newIdcard); //Output 2
?>
get_defined_vars()
This function is very useful when debugging a program. It will return an array containing all defined variables, including environment, system and user-defined variables.
The code is as follows:
var_dump(get_defined_vars());
?>
escapeshellcmd()
This function is used to skip special symbols in the string to prevent malicious users from playing tricks to crack the server system. Can be used with the exec() and system() functions.
The code is as follows:
$command = './configure '.$_POST['configure_options'];
$escaped_command = escapeshellcmd($command);
system($escaped_command);
?>
checkdate()
This function can be used to check the validity of date parameters. It can verify the validity of each parameter entered.
The code is as follows:
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));
//Output
//bool(true)
//bool(false)
?>
php_strip_whitespace()
This function will return the PHP source code with comments and spaces removed. This is useful for comparing the actual amount of code versus the number of comments.
The code is as follows:
// Note 1
/*
* Note 2
*/
echo php_strip_whitespace(__FILE__);
do_nothing();
?>