10 unusual but very useful functions in PHP

  • 2020-03-31 20:28:15
  • OfStack

1. Sys_getloadavg ()
Sys_getloadavt () can get the system load. This function returns an array of three elements, each representing the average load of the system over the last 1, 5, and 15 minutes, respectively.
Instead of letting the server go down because the load is too high, take the initiative to die a script when the load is high. Sys_getloadavg () is there to help you do this. Unfortunately, this function is not valid under Windows.
2. Pack ()
Pack() saves storage by converting the 32-bit hexadecimal string returned by md5() into a 16-bit binary string.
3. The cal_days_in_month ()
Cal_days_in_month () can return how many days there are in a given month.
4. _ ()
WordPress developers often see this function, along with _e (). Both functions have the same functionality and, when combined with the gettext() function, allow for a multilingual site. See the relevant section of the PHP manual for details.
5. Get_browser ()
Wouldn't it be nice to see what the user's browser can do before sending the page? Get_browser () gets the user's browser type and what the browser supports, but first you'll need a php_browscap.ini file to reference the function.
Note that this function determines the functionality of the browser based on the general characteristics of the browser class. For example, if the user turns off JavaScript support in the browser, the function won't know. However, it is accurate in determining the browser type and OS platform.
6. Debug_print_backtrace ()
This is a debugging function to help you find logic errors in your code. To understand this function, let's look at an example:
 
$a = 0; 
function iterate() { 
global $a; 
if( $a < 10 ) 
recur(); 
echo $a . ", "; 
} 
function recur() { 
global $a; 
$a++; 
// how did I get here? 
echo "nnn " ; 
debug_print_backtrace(); 
if( $a < 10 ) 
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 iterate() 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_stuffindex.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. The metaphone ()
This function returns the metaphone value of the word, and words with the same pronunciation have the same metaphone value, which means that this function can help you determine whether two words are pronounced the same way. But not for Chinese...
The natsort ()
Natsort () can arrange an array in a natural sort way. Here's an example:
 
$items = array( 
 " 100 apples " ,  " 5 apples " ,  " 110 apples " ,  " 55 apples "  
); 
// normal sorting: 
sort($items); 
print_r($items); 
# Outputs: 
# Array 
# ( 
# [0] => 100 apples 
# [1] => 110 apples 
# [2] => 5 apples 
# [3] => 55 apples 
# ) 
natsort($items); 
print_r($items); 
# Outputs: 
# Array 
# ( 
# [2] => 5 apples 
# [3] => 55 apples 
# [0] => 100 apples 
# [1] => 110 apples 
# ) 

9. The levenshtein ()
Levenshtein() tells you the "distance" between two words. It tells you how many letters to insert, replace, and delete if you want to change one word into another.
Here's 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? " ; 

The glob ()
Glob () makes it seem silly to use opendir(), readdir(), and closedir() to find files.
 
foreach (glob( " *.php " ) as $file) 
echo  " $filen " ; 

Related articles: