Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Unusual but useful PHP function sharing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "unusual but practical PHP function sharing". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Sys_getloadavg ()

Sys_getloadavt () can obtain the load of the system. This function returns an array of three elements, each representing the average load of the system over the past 1, 5, and 15 minutes, respectively. Instead of letting the server crash due to high load, take the initiative to die a script when the system load is high. Sys_getloadavg () is used to help you achieve this function. Unfortunately, this function is not valid under windows.

2. Pack ()

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

3. Cal_days_in_month ()

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

4. ()

WordPress developers often see this function, as well as _ e (). These two functions have the same function, and when used in conjunction with the gettext () function, the website can be multilingual. For details, please refer to the introduction of the relevant section of the PHP manual.

5. Get_browser ()

Isn't it nice to see what the user's browser can do before sending the page? Get_browser () can get the user's browser type and the features supported by the browser, but first you need a php_browscap.ini file to use as a reference file for the function.

Note that this function's judgment of browser functionality is based on the general characteristics of this type of browser. For example, if the user turns off the browser's support for JavaScript, the function does not know this. However, this function is very accurate in determining the browser type and the 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, let's just 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 "\n\n\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() 这个函数返回单词的metaphone值,相同读音的单词具有相同的metaphone值,也就是说这个函数可以帮你判断两个单词的读音是否 相同。 8. natsort() natsort()能将一个数组以自然排序法 进行排列,直接看个例子吧: $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. 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 change one word into another.

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 () makes you feel stupid to use opendir (), readdir (), and closedir () to find files.

Foreach (glob ("* .php") as $file) echo "$file\ n"; "unusual but useful PHP function sharing" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report