10 little-known but very useful functions in PHP. PHP has a rich set of built-in functions that we have used. However, most of us are not familiar with, but they are very useful. In this article, I listed a rich set of built-in functions in PHP. many of them have been used, but many of them are still unfamiliar to us, but they are very useful. In this article, I have listed some little-known PHP functions that will highlight your eyes.
Levenshtein ()
Have you ever experienced how different the two words are? this function helps you solve this problem. It can compare different degrees of two strings.
Usage:
$ Str1 = "carrot ";
$ Str2 = "carrrott ";
Echo levenshtein ($ str1, $ str2); // Outputs 2
?>
Get_defined_vars ()
This is a very useful function in debug debugging. This function returns a multi-dimensional array containing all defined variables.
Usage:
Print_r (get_defined_vars ());
?>
Php_check_syntax ()
This function is very useful and can be used to check whether PHP syntax is correct. Technically, this function has been deleted since PHP 5.05.
Usage:
$ 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 ";
}
?>
Ignore_user_abort ()
This function is used to deny the browser user's request to terminate the script execution. Under normal circumstances, the exit of the client will cause the script on the server to stop running.
Usage:
Ignore_user_abort ();
?>
Highlight_string ()
When you want to display PHP code to the page, the highlight_string () function is very useful. This function will highlight the defined color of your PHP code with the built-in PHP syntax. This function has two parameters. The first parameter is a string, indicating that the string needs to be highlighted. If the second parameter is set to TRUE, the highlighted code is returned as the return value.
Usage
Highlight_string ('');
?>
Highlight_file
This is a very useful PHP function. it can return the specified PHP file and highlight the file content in a highlighted color according to syntax. The highlighted codes are all processed with HTML tags.
Usage:
Highlight_file ("php_script.php ");
?>
Php_strip_whitespace
This function is similar to the show_source () function, but it deletes comments and space characters in the file.
Usage:
Echo php_strip_whitespace ("php_script.php ");
?>
Get_browser
This function reads the browscap. ini file and returns browser compatibility information.
Usage:
Echo $ _ SERVER ['http _ USER_AGENT '];
$ Browser = get_browser ();
Print_r ($ browser );
?>
Memory_get_usage (), memory_get_peak_usage (), getrusage ()
These functions are used to obtain the memory and CPU usage. the memory_get_usage () function returns the memory usage, the memory_get_peak_usage () function returns the memory usage peak, and the getrusage () function returns the CPU usage, when debugging PHP code performance, these functions provide you with some useful information. Note that Window is invalid in these functions.
Usage:
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 ratio can reach about 50%. In addition, the gzencode () and gzdecode () functions can achieve similar results, but different compression algorithms are used.
Usage:
$ 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 presponorci,
Non ultricies elit lacus quis ante. Lorem ipsum dolor
Sit amet, consectetur adipiscing elit. Aliquam
Prepolicullamcorper 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 );
?>
Bytes. In this article, I listed one...