10 little-known but very useful functions in PHP _ PHP Tutorial

Source: Internet
Author: User
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.


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


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.


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


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.


Highlight_string ('');



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.


Highlight_file ("php_script.php ");



This function is similar to the show_source () function, but it deletes comments and space characters in the file.


Echo php_strip_whitespace ("php_script.php ");



This function reads the browscap. ini file and returns browser compatibility information.


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.


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.


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.