PHP has many built-in functions, most of which are widely used by programmers. But there are some functions hidden in the corner, this article will introduce you to 7 little-known, but very useful functions. A programmer who doesn't use it may wish to come and see.
1.highlight_string ()
The highlight_string () function becomes very useful when you need to present PHP code in a Web site. The function outputs or returns a syntax-highlighting version of a given PHP code by using the color defined in the PHP syntax highlighting program.
Example:
123 |
<?php highlight_string( ‘<?php phpinfo(); ?>‘ ); ?> |
2.str_word_count ()
The function must pass a parameter that returns the number of words based on the parameter type. As shown in the following:
1234 |
<?php $str = "How many words do I have?" ; echo str_word_count ( $str ); //Outputs 6 ?> |
3.levenshtein ()
The function mainly returns the Levenshtein distance between two strings. Levenshtein distance, also known as the editing distance, refers to the minimum number of edit operations required between two strings, converted from one to another. Permission edits include replacing one character with another character, inserting a character, and deleting a character. This function is useful for finding typos submitted by users.
Example:
12345 |
<?php $str1 = "carrot" ; $str2 = "carrrott" ; echo levenshtein( $str1 , $str2 ); //Outputs 2 ?> |
4.get_defined_vars ()
The function returns a multidimensional array that contains a list of all defined variables, including environment variables, server variables, and user-defined variables.
Example:
1 |
print_r(get_defined_vars()); |
5.escapeshellcmd ()
This function avoids the special symbols in the string, which prevents the user from playing tricks to crack the server system. You can use this function with EXEC () or system () two functions, which can reduce malicious behavior of online users.
Example:
12345 |
<?php $command = ‘./configure ‘ . $_POST [ ‘configure_options‘ ]; $escaped_command = escapeshellcmd ( $command ); system( $escaped_command ); ?> |
6.checkdate ()
This function can be used to check if a date is valid, for example, 0-32,767 years, 1-12 months, and days with month and leap year.
Example:
1234567 |
<?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 for deleted PHP comments and whitespace characters, which is useful for comparing the actual number of code with the number of comments.
Example:
123456789 |
<?php // PHP comment here /* * Another PHP comment */ echo php_strip_whitespace( __FILE__ ); // Newlines are considered whitespace, and are removed too: do_nothing(); ?> |
Output Result:
12 |
<?php echo php_strip_whitespace( __FILE__ ); do_nothing(); ?> |
7 little-known but super-useful PHP functions--turn (Qi ba Jiu 0)