Overview
PHP has a number of built-in functions, most of which are used by developers. But there are some equally useful but forgotten in the corner, this article will introduce 7 little-known features but very cool functions.
Highlight_string ()
The highlight_string () function is useful when you need to display PHP code in a Web page. This function returns the highlighted version of the code in the function through the color defined in PHP.
Copy Code code as follows:
<?php
Highlight_string (' <?php echo ' Hello World;?> ');
echo highlight_string (' <?php echo ' Hello World;?> ', true);
?>
Str_word_count ()
This function makes it easy to return the number of words in the input string argument.
Copy Code code as follows:
<?php
$str = "Hello World";
echo Str_word_count ($STR); Output 2
?>
Levenshtein ()
The function can easily return the Levenshtein (edit distance) between two parameters. Once encountered a need, users in the editing of identity cards, restricting users can only modify 4 digits, using this function.
Copy Code code as follows:
<?php
$idcard = ' 230406198506206797 ';
$newIdcard = ' 230406198506207798 ';
Echo Levenshtein ($idcard, $newIdcard); Output 2
?>
Get_defined_vars ()
This function is useful when debugging a program, and it returns an array of all the defined variables, including the environment, system, and user-defined variables.
Copy Code code as follows:
<?php
Var_dump (Get_defined_vars ());
?>
Escapeshellcmd ()
This function is used to skip the special symbols in the string to prevent malicious users from playing tricks on the server system. Can be used with the Exec () and system () functions.
Copy Code code as follows:
<?php
$command = './configure '. $_post[' configure_options '];
$escaped _command = Escapeshellcmd ($command);
System ($escaped _command);
?>
Checkdate ()
This function can be used to detect the validity of date parameters. It verifies the legality of each parameter entered.
Copy Code code as follows:
<?php
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 after the deletion of the annotation and space. This is useful for comparing the actual number of code with the number of comments.
Copy Code code as follows:
<?php
Note 1
/*
* NOTE 2
*/
echo Php_strip_whitespace (__file__);
Do_nothing ();
?>