10 little-known but very useful PHP functions

Source: Internet
Author: User
This article introduces the content is about 10 little-known but very useful PHP functions, has a certain reference value, now share to everyone, the need for friends can refer to

1. Historical and historical distance (string similarity)


<?PHP$STR1 = "AAA"; $str 2 = "Aaab"; Echo Levenshtein ($str 1, $str 2); Output 2?>



It can calculate a few letters between the string str1 and the string str2. This is a PHP feature, because you are likely to use other languages to implement the matrix element of historical and historical distance.

2. Returns an array of all the variables defined


<?phpprint_r (Get_defined_vars ());? >


This is a very useful feature when you want to debug your code while the code is confused about which variables are defined. It returns an array of all the variables that are defined. Put in the last sentence Oh ~ ~ ~

3. Check your PHP code syntax function


<?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 con tained no syntax errors ";  }? >


Php_check_syntax is also the function. However, in the php5 is discarded, or use PHP to develop an online compiler is still very useful, of course, you can use PHP4 also capable.

4. Ignore abort requests from the client


<? Phpignore_user_abort ();? >


In general, the user can stop all requests in the browser by pressing "Stop", but this function can ignore the user's action.

5. Highlighting syntax Markers


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


This is actually more useful to do some specific syntax display, it will automatically output <?php AA ()?> string, and display blue represents highlighting.

6.highlight_file () Highlight file content syntax tag


<?phphighlight_file ("test.php"); Here must be an existing filename?>


This function will load the contents of the test.php file, and then the <?php ....?> inside the content to highlight the display, very cool ~ ~ ~

7. Remove comments and spaces from the file


<?php  Echo php_strip_whitespace ("test.php");? >


This function is huge, if you do not want your 2B colleague to steal the test.php after you finish writing the program, you can use this function to output the program that removes comments and spaces to the page, and then copy it back. Like the compression function.

8. Read the client browser version information on the server side


<?phpecho $_server[' http_user_agent ']; Output common browser information such as mozilla/5.0 (Windows NT 5.2; rv:26.0) gecko/20100101 firefox/26.0$browser = Get_browser (); Print_r ($browser);? >


Note: Browscap.ini file please go to the Internet to download the latest, the file contains all the existing browser type and its information, and in php.ini Add the file point configuration. This file can help you to more accurately determine the user's current browser version information.

9. Check performance and CPU usage efficiency




Try it out and check to see if the code you're writing is ravaging the server. Unfortunately, these functions are not available on Windows servers. (In fact, it's normal.)

10. Compressing String functions


$string = "Put a lot of Chinese characters here, anyway it's very long and long"; $compressed = Gzcompress ($string);//compress the echo $compressed; It's garbled. is gzip compression code $original = gzuncompress ($compressed); Unzip the echo $original;//Normal


This function is useful, for example, if you want to write some Ajax in the client and the server without moral integrity interaction, appropriate to use some of this function is very good. (The function is good, but do not mess with, too often on the server performance is also affected)

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.