10 little-known but very useful functions in PHP

Source: Internet
Author: User

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:

123456 <?php$str1= "carrot";$str2 = "carrrott";echo levenshtein($str1, $str2); //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:

123 <?phpprint_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:

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

123 <?phpignore_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

123 <?phphighlight_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:

123 <?phphighlight_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:

123 <?phpechophp_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:

12345 <?<span id="6_nwp" style="width: auto; height: auto; float: none;"><a id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=2061f523ace1e686&k=php&k0=php&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=86e6e1ac23f56120&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3747%2Ehtml&urlid=0" target="_blank" mpid="6" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">php</span></a></span>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:

123456789101112 <?<span id="3_nwp" style="width: auto; height: auto; float: none;"><a id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=2061f523ace1e686&k=php&k0=php&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=86e6e1ac23f56120&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3747%2Ehtml&urlid=0" target="_blank" mpid="3" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">php</span></a></span>echo "Initial: ".memory_get_usage()." bytes \n";echo "Peak: ".memory_get_<span id="4_nwp" style="width: auto; height: auto; float: none;"><a id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=2061f523ace1e686&k=peak&k0=peak&kdi0=0&luki=1&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=86e6e1ac23f56120&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3747%2Ehtml&urlid=0" target="_blank" mpid="4" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">peak</span></a></span>_usage()." bytes \n";$<span id="5_nwp" style="width: auto; height: auto; float: none;"><a id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=2061f523ace1e686&k=data&k0=data&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=86e6e1ac23f56120&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3747%2Ehtml&urlid=0" target="_blank" mpid="5" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">data</span></a></span> = 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:

1234567891011121314151617181920212223 <?<span id="1_nwp" style="width: auto; height: auto; float: none;"><a id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=2061f523ace1e686&k=php&k0=php&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=86e6e1ac23f56120&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3747%2Ehtml&urlid=0" target="_blank" mpid="1" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">php</span></a></span>$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 <span id="2_nwp" style="width: auto; height: auto; float: none;"><a id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=2061f523ace1e686&k=leo&k0=leo&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=86e6e1ac23f56120&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3747%2Ehtml&urlid=0" target="_blank" mpid="2" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">leo</span></a></span> 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);?>

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

English Original: Ten little known but useful PHP functions

10 little-known but very useful functions in PHP

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.