Home >Backend Development >PHP Tutorial >7 little-known but very practical PHP functions, little-known php_PHP tutorial
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()
The highlight_string() function is very useful when PHP code needs to be displayed on a web page. This function returns a highlighted version of the code in the function using the colors defined built-in to PHP.
Copy code 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.
Copy code 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.
Copy code 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.
Copy code The code is as follows:
var_dump(get_defined_vars());
?>
escapeshellcmd()
This function is used to skip special symbols in strings to prevent malicious users from playing tricks to crack the server system. Can be used with the exec() and system() functions.
Copy code 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.
Copy code 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.
Copy code The code is as follows:
// Note 1
/*
* Note 2
*/
echo php_strip_whitespace(__FILE__);
do_nothing();
?>