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, I've listed some little-known PHP functions that 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 if PHP is syntactically correct. For technical reasons, starting with PHP 5.05, this function was removed.
Usage:
- <?PHP&NBSP;
- $error _message =
- $filename =
- if (!php_check_syntax ( $filename, & $error _message)) {
Li class= "alt" > echo
- } else {
- echo < Span class= "string" > "the file $filename contained no syntax errors";
- }
- ?>&NBSP;
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 display the PHP code on the page, the highlight_string()
function becomes very useful. 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 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 show_source()
similar to the previous 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()
functions return memory usage, memory_get_peak_usage()
functions return memory usage spikes, getrusage () returns cup usage, and these functions provide you with useful information when debugging PHP code performance. 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);
- ?>