PHP Tips: A few more useful PHP functions

Source: Internet
Author: User
Tags foreach glob pack valid

1. Sys_getloadavg ()

SYS_GETLOADAVT () can obtain the system load condition. The function returns an array of three elements, each representing the average load of the system in the past 1, 5, and 15 minutes, respectively.

Rather than letting the server down because it is overloaded, it is better to actively die 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 () Converts the 32-bit 16-digit string returned by MD5 () to a 16-bit binary string, which saves storage space.

3. Cal_days_in_month ()

Cal_days_in_month () can return the number of days in the specified month.

4. _ ()

WordPress developers can often see this function, as well as _e (). These two functions are identical and can be used in conjunction with the GetText () function to realize the multi-language of the website. Refer to the relevant sections of the PHP manual for details.

5. Get_browser ()

Is it good to look at what the user's browser can do before sending the page? Get_browser () can get the user's browser type, as well as browser-supported features, but first you need a Php_browscap.ini file to make reference files for functions.

Note that this function is based on the general nature of the browser's functionality. For example, if a user turns off browser support for JavaScript, the function is not aware of this. However, the function is accurate in determining 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 look at an example:

$a = 0; 
Function iterate () { 
Global $a; 
if ($a < ten)  
recur (); 
echo $a. ","; 

Function recur () { 
Global $a; 
$a ++; 
//How did I get here?& nbsp
echo "nnn"; 
Debug_print_backtrace (); 
if ($a <)  
Iterate (); 

Iterate (); 
# output: 
#0 recur () called at [c:htdocsphp_stuffindex.php:8] 
#1 iterate ( Called at [c:htdocsphp_stuffindex.php:25] 
#0 recur () called at [c:htdocsphp_stuffindex.php:8] 
#1 I Terate () called at [c:htdocsphp_stuffindex.php:21] 
#2 recur () called at [c:htdocsphp_stuffindex.php:8] 
#3 Iterate () called at [c:htdocsphp_stuffindex.php:25] 
#0 recur () called at [C:htdocsphp_stuffindex.php:8 ] 
#1 Iterate () called at [c:htdocsphp_stuffindex.php:21] 
#2 recur () called at [C:htdocsphp_stuffinde x.php:8] 
#3 Iterate () called at [c:htdocsphp_stuffindex.php:21] 
#4 recur () called at [C:htdocsphp_stuffindex.php:8 ] 
#5 Iterate () called at [c:htdocsphp_stuffindex.php:25]  

7. Metaphone ()

This function returns the Metaphone value of the word, the same pronunciation of the word has the same Metaphone value, that is, this function can help you determine whether two words are the same pronunciation. But it's not valid for Chinese ...

8. Natsort ()

Natsort () can arrange an array in a natural sort order, just look 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 two words. It tells you how many letters you need to insert, replace, and delete if you want to turn a word into another word.

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

Glob ()

Glob () makes you feel stupid to find files using Opendir (), Readdir () and Closedir ().

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

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.