10 of the PHP built-in functions you don't necessarily know

Source: Internet
Author: User
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$STR1 = "carrot"; $str 2 = "Carrrott"; Echo Levenshtein ($str 1, $str 2); Outputs 2?>

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:

<?phpprint_r (Get_defined_vars ());? >

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

<?phpignore_user_abort ();? >

Highlight_string ()

The highlight_string () function is useful when you want to display the PHP code 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

<?phphighlight_string (' <?php phpinfo ();?> ');? >

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:

<?phphighlight_file ("php_script.php");? >

Php_strip_whitespace

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

Usage:

<?phpecho php_strip_whitespace ("php_script.php");? >

Get_browser

This function reads the Browscap.ini file and returns the browser-compatible information.

Usage:

<?phpecho $_server[' http_user_agent '; $browser = Get_browser ();p rint_r ($browser);? >

Memory_get_usage (), Memory_get_peak_usage (), Getrusage ()

These functions are used to obtain memory and CPU usage, the Memory_get_usage () function returns memory usage, the Memory_get_peak_usage () function returns the peak memory usage, getrusage () returns the cup usage, These functions will provide you with some useful information when debugging PHP code performance. But one thing to note is that in these functions, the window is not valid.

Usage:

<?phpecho "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, consecteturadipiscing elit. Nunc ut elit id mi ultriciesadipiscing. Nulla Facilisi. Praesent pulvinar,sapien vel feugiat vestibulum, nulla dui pretium orci,non ultricies elit lacus quis ante. Lorem ipsum dolorsit amet, consectetur adipiscing elit. Aliquampretium ullamcorper Urna quis iaculis. Etiam ac massased turpis tempor luctus. Curabitur sed nibh eu elitmollis congue. Praesent ipsum diam, Consectetur vitaeornare A, aliquam a nunc. In ID magna pellentesquetellus posuere adipiscing. Sed non mi metus, at Laciniaaugue. Sed magna Nisi, Ornare in mollis in, mollissed nunc. Etiam at Justo in Leo Congue mollis. Nullam in Neque eget metus hendrerit scelerisqueeu non enim. Ut malesuada lacus eu nulla bibendumid euismod urna sodales. "; $compressed = gzcompress ($string); $original = Gzuncompress ($compressed);?>
  • 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.