Home  >  Article  >  Backend Development  >  7 Little-Known But Super Useful PHP Functions_PHP Tutorial

7 Little-Known But Super Useful PHP Functions_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:38:58730browse

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.

1.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.

Usage:

<?php
highlight_string(&#39;<?php phpinfo(); ?>&#39;);
?>
2.str_word_count()

This function can conveniently return the number of words in the input string parameter.

Usage:

?php
$str = "How many words do I have?";
echo str_word_count($str); //Outputs 5
?>
3.levenshtein()

When you need to track the user's spelling errors, this function can conveniently return the levenshtein (edit distance) between the two parameters.

Usage:

<?php
$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); //Outputs 2
?>
4.get_defined_vars()

This function is very useful when debugging a program. It returns an array containing all defined variables, including environment, system and user-defined variables.

Usage:

print_r(get_defined_vars());

5.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.

Usage:

<?php
$command = &#39;./configure &#39;.$_POST[&#39;configure_options&#39;];

$escaped_command = escapeshellcmd($command);
 
system($escaped_command);
?>
6.checkdate()

This function can be used to check the validity of date parameters. It can verify the validity of each parameter entered.


Usage:

<?php
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));

//Output
//bool(true)
//bool(false)
?>
7.php_strip_whitespace()

This function returns the PHP source code with comments and spaces removed. This is useful for comparing the actual amount of code versus the number of comments.

Usage:

<?php
// PHP comment here

/*
 * Another PHP comment
 */

echo        php_strip_whitespace(__FILE__);
// Newlines are considered whitespace, and are removed too:
do_nothing();
?>

The output:

<?php
 echo php_strip_whitespace(__FILE__); do_nothing(); ?>



================================================== ========

PHP WeChat platform is open!
Search "PHPChina" on WeChat and click the follow button to get the latest and most professional industry information pushed by PPC for you. There are also special columns for you
[PPC Mining]: From time to time, we will provide you with stories about classic products and product people.
[PPC Foreign Language]: Share a foreign language translation article every day

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735055.htmlTechArticlePHP has many 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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn