PHP Training Tutorials 10 little-known but very useful functions in PHP
PHP has a very rich built-in function, many of which we have used, but still have a lot of functions we are not familiar with, but they are very useful. In this article, the brother even made a list of PHP functions that are little known but will make your eyes bright .
Levenshtein ()
Have you ever experienced the need to know how different two words are, this function is to help you solve this problem. It can compare the different degrees of two strings.
Usage:
<?php
$str 1 = "carrot";
$str 2 = "Carrrott";
Echo Levenshtein ($str 1, $str 2); Outputs 2
?>
source:http://php.net/manual/en/function.levenshtein.php
Get_defined_vars ()
This is a very useful function in debug debugging. This function returns a multidimensional array that contains all the variables that have been defined.
Usage:
<?php
Print_r (Get_defined_vars ());
?>
source:http://php.net/manual/en/function.get-defined-vars.php
Php_check_syntax ()
This function is very useful and can be used to check PHP is syntactically correct. For technical reasons, starting with PHP 5.05, this function was removed.
Usage:
<?php
$error _message = "";
$filename = "./php_script.php";
if (!php_check_syntax ($filename, & $error _message)) {
echo "Errors were found in the file $filename: $error _message";
} else {
echo "The file $filename contained no syntax errors";
}
?>
source:http://www.php.net/manual/en/function.php-check-syntax.php
Ignore_user_abort ()
This function denies the browser-side user from terminating the request to execute the script. Under normal circumstances, the client's exit causes the server-side script to stop running.
Usage:
<?php
Ignore_user_abort ();
?>
source:http://www.php.net/manual/en/function.ignore-user-abort.php
Highlight_string ()
when you want to put The highlight_string () function appears useful when the PHP code is displayed on the page. This function will highlight the PHP code you provided with the built-in PHP syntax highlighting the defined color. This function has two parameters, the first argument is a string that indicates that the string needs to be highlighted. If the second parameter is set to True, the function will return the highlighted code as the return value.
Usage
<?php
Highlight_string (' <?php phpinfo ();?> ');
?>
source:http://php.net/manual/en/function.highlight-string.php
Highlight_file
This is a very useful PHP function that returns the specified PHP file and highlights the contents of the file in a highlighted color according to the syntax semantics. The highlighted code is handled with HTML tags.
Usage:
<?php
Highlight_file ("php_script.php");
?>
source:http://www.php.net/manual/en/function.highlight-file.php
Php_strip_whitespace
This function is similar to the previous show_source () function, but it deletes comments and whitespace in the file.
Usage:
<?php
Echo php_strip_whitespace ("php_script.php");
?>
source:http://www.php.net/manual/en/function.php-strip-whitespace.php
Get_browser
This function reads the Browscap.ini file and returns the browser-compatible information.
Usage:
<?php
echo $_server[' http_user_agent '];
$browser = Get_browser ();
Print_r ($browser);
?>
source:http://www.php.net/manual/en/function.get-browser.php
Memory_get_usage (), Memory_get_peak_usage (), Getrusage ()
These functions are used to obtain memory and CPU usage, memory_get_usage () function returns memory usage, Memory_get_peak_usage () function returns memory usage spikes, getrusage () returns cup usage, when debugging PHP code performance, These functions will provide you with some useful information. But one thing to note is that in these functions, the window is not valid.
Usage:
<?php
echo "Initial:". Memory_get_usage (). "Bytes \ n";
echo "Peak:". Memory_get_peak_usage (). "Bytes \ n";
$data = Getrusage ();
echo "User time:".
($data [' ru_utime.tv_sec '] +
$data [' ru_utime.tv_usec ']/1000000);
echo "System time:".
($data [' ru_stime.tv_sec '] +
$data [' ru_stime.tv_usec ']/1000000);
?>
Gzcompress (), gzuncompress ()
These two functions are used to compress and decompress string data. Their compression rate can reach about 50%. Other functions Gzencode () and Gzdecode () can achieve similar results, but different compression algorithms are used.
Usage:
<?php
$string =
"Lorem ipsum dolor sit amet, consectetur
Adipiscing elit. Nunc ut elit id mi ultricies
Adipiscing. Nulla Facilisi. Praesent Pulvinar,
Sapien vel feugiat vestibulum, nulla dui pretium orci,
Non ultricies elit lacus quis ante. Lorem ipsum dolor
Sit amet, consectetur adipiscing elit. Aliquam
Pretium ullamcorper Urna quis iaculis. Etiam ac Massa
Sed turpis tempor luctus. Curabitur sed nibh eu elit
Mollis Congue. Praesent ipsum diam, Consectetur vitae
Ornare A, aliquam a nunc. In ID magna pellentesque
Tellus posuere adipiscing. Sed non mi metus, at Lacinia
Augue. Sed magna Nisi, Ornare in mollis in, mollis
Sed nunc. Etiam at Justo in Leo Congue mollis.
Nullam in Neque eget metus hendrerit scelerisque
EU non enim. Ut malesuada lacus eu nulla bibendum
ID euismod urna sodales. ";
$compressed = gzcompress ($string);
$original = gzuncompress ($compressed);
?>
PHP Training Tutorial 10 little-known but very useful functions in PHP