10 classic PHP function_php tutorials

Source: Internet
Author: User
Tags glob
10 classic PHP functions. 1. sys_getloadavg () sys_getloadavt () can obtain the system load. This function returns an array containing three elements. each element represents the last 1, 5, and 15 minutes of the system.

1. sys_getloadavg ()

Sys_getloadavt () can obtain the system load. This function returns an array containing three elements, each representing the average load of the system in the past 1, 5, and 15 minutes.

Instead of causing the server to go down due to overload, it is better to take the initiative to die out a script when the system load is very high. sys_getloadavg () is used to help you implement this function. But unfortunately, this function is invalid in windows.

2. pack ()

Pack () can convert a 32-bit hexadecimal string returned by md5 () to a 16-bit binary string, saving storage space.

3. cal_days_in_month ()

Cal_days_in_month () returns the total number of days in a specified month.

4 ._()

WordPress developers often see this function as well as _ e (). These two functions have the same functions and can be used in combination with the gettext () function to implement multilingual website operations. For more information, see the relevant section of the PHP Manual.

5. get_browser ()

Before sending the page, check what your browser can do? Get_browser () can obtain the browser type and functions supported by the browser. However, you need a php_browscap.ini file to provide a reference file for the function.

Note that this function is based on the general features of the browser. For example, if the user closes the browser's support for JavaScript, the function cannot know this. However, this function is very accurate in determining the browser type and OS platform.

6. debug_print_backtrace ()

This is a function used for debugging and can help you find logical errors in the code. To solve this function, let's look at an example:

$ A = 0;
Function iterate (){
Global $;
If ($ a <10)
Recur ();
Echo $ a. ",";
}
Function recur (){
Global $;
$ A ++;
// How did I get here?
Echo "\ n ";
Debug_print_backtrace ();
If ($ a <10)
Iterate ();
}
Iterate ();
# OUTPUT:
#0 recur () called at [C: \ htdocs \ php_stuff \ index. php: 8]
#1 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: 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 () called 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 a word. words with the same pronunciation have the same metaphone value. that is to say, this function can help you determine whether the two words have the same pronunciation. However, it is invalid for Chinese characters...

8. natsort ()

Natsort () can sort an array by natural sorting. let's look at an example:

$ Items = array (
"100 apples", "5 apples", "110 apples", "55 apples"
);
// Normal sorting:
Sort ($ items );
Print_r ($ items );
# Outputs:
# Array
#(
# [0] = & gt; 100 apples
# [1] = & gt; 110 apples
# [2] => 5 apples
# [3] => 55 apples
#)
Natsort ($ items );
Print_r ($ items );
# Outputs:
# Array
#(
# [2] => 5 apples
# [3] => 55 apples
# [0] = & gt; 100 apples
# [1] = & gt; 110 apples
#)

9. levenshtein ()

Levenshtein () tells you the "distance" between two words ". It tells you how many letters need to be inserted, replaced, and deleted if you want to change one word to another.

Let's look at an example:

$ Dictionary = array (
"Php", "javascript", and "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 () makes you think it is silly to use opendir (), readdir () and closedir () to find files.

Foreach (glob ("*. php") as $ file)
Echo "$ file \ n ";

Http://www.bkjia.com/PHPjc/824904.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/824904.htmlTechArticle1. sys_getloadavg () sys_getloadavt () can obtain the system load. This function returns an array containing three elements. each element represents the last 1, 5, and 15 minutes of the system...

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.