10 little-known but very useful functions in PHP

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 $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 $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 ()

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

<?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 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, 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:

<?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);  ? >

Do you also think of any other useful functions? Please share it in the comments!

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