10 Very useful PHP functions to share

Source: Internet
Author: User
Tags glob
Functions are a powerful source of PHP, but many PHP functions are not fully utilized. Here, we give you a brief introduction to 10 less common, but very useful functions.

1, Sys_getloadavg ()

SYS_GETLOADAVT () can obtain the system load condition. The function returns an array of three elements, each representing the average load for the system in the past 1, 5, and 15 minutes. Instead of letting the server down because of high load, it is better to die out a script when the system is heavily loaded, Sys_getloadavg () is used to help you achieve this function. Unfortunately, this function is not valid under Windows.

2. Pack ()

Pack () can save storage space by converting the 32-bit 16 binary string returned by MD5 () to a 16-bit binary string.

3, Cal_days_in_month ()

Cal_days_in_month () is able to return the number of days in a specified month.

4, _ ()

WordPress developers often see this function, as well as _e (). The functions of the two functions are the same and are used in conjunction with the GetText () function to enable multilingual Web sites. Refer to the relevant section of the PHP manual for details.

5, Get_browser ()

Before sending the page to see what the user's browser can do is not very good? Get_browser () can get the user's browser type, as well as browser-supported features, but first you need a Php_browscap.ini file for the function to make reference files.

It is important to note that the function of the browser is based on the general characteristics of the browser. For example, if a user turns off browser support for JavaScript, the function is not aware of this. However, the function is accurate in judging the browser type and OS platform.

6, Debug_print_backtrace ()

This is a debugging function that can help you find logic errors in your code. To understand this function, just take a look at the example:

$a = 0;   function iterate () {global $a;   if ($a <) recur (); echo $a.   “, “;   } function recur () {global $a;   $a + +;   How do I get here?   echo "\n\n\n";   Debug_print_backtrace ();   if ($a <) iterate ();   } iterate (); # OUTPUT: #0 recur () called at [C:\htdocs\php_stuff\index.php:8] #1 iterate () called at [C:\htdocs\php_stuff\index.php   : #0 recur () called at [C:\htdocs\php_stuff\index.php:8] #1 iterate () called at [c:\htdocs\php_stuff\index.php:21] #2 recur () called at [C:\htdocs\php_stuff\index.php:8] #3 iterate () called at [c:\htdocs\php_stuff\index.php:25] #0 Recur () called at [C:\htdocs\php_stuff\index.php:8] #1 iterate () called at [c:\htdocs\php_stuff\index.php:21] #2 recur () called at [C:\htdocs\php_stuff\index.php:8] #3 iterate () called at [c:\htdocs\php_stuff\index.php:21] #4 recur () CA Lled at [C:\htdocs\php_stuff\index.php:8] #5 iterate () called at [c:\htdocs\php_stuff\index.php:25]

7, Metaphone ()

This function returns the Metaphone value of the word, with the same pronounced word having the same Metaphone value, which means that the function can help you determine whether the two words are pronounced the same.

8, Natsort ()

Natsort () can arrange an array in a natural sort order, looking directly at an example:

$items = Array (   "apples", "5 apples", "apples", "apples"   );   Normal sorting:   sort ($items);   Print_r ($items);   # Outputs:   # Array   # (   # [0] = + Apples   # [1] = Apples   # [2] = 5 apples   # [3] = Apples   #)   Natsort ($items);   Print_r ($items);   # Outputs:   # Array   # (   # [2] = 5 apples   # [3] = apples   # [0] = + Apples   # [ 1] = Apples   #)

9, Levenshtein ()

Levenshtein () tells you the "distance" between the two words. It tells you how many letters you need to insert, replace, and delete if you want to turn one word into another.

Let's look at an example:

$dictionary = Array (   "PHP", "JavaScript", "CSS"   );   $word = "japhp";   $best _match = $dictionary [0];   $match _value = Levenshtein ($dictionary [0], $word);   foreach ($dictionary as $w) {   $value = Levenshtein ($word, $w);   if ($value < $match _value) {   $best _match = $w;   $match _value = $value;   }   }   echo "Did you mean the ' $best _match ' category?";

10, Glob ()

Glob () will make you feel stupid to find files with Opendir (), Readdir () and Closedir ().

foreach (Glob ("*.php") as $file)   echo "$file \ n";

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.