Home  >  Article  >  Backend Development  >  Introducing 10 very useful PHP functions_PHP Tutorial

Introducing 10 very useful PHP functions_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 10:57:25675browse

The

function is the source of such power for PHP, but many PHP functions are not fully utilized. Here, we briefly introduce 10 uncommon but very useful functions.

1. sys_getloadavg()

sys_getloadavt() can obtain the system load status. This function returns an array containing three elements, each element representing the average load of the system over the past 1, 5, and 15 minutes. Rather than letting the server crash due to excessive load, it is better to actively die a script when the system load is very 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. Save storage space.

3. cal_days_in_month()

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

4. _()

WordPress developers often see this function, as well as _e(). These two functions have the same function and can be used in combination with the gettext() function to achieve multilingualization of the website. For details, please refer to the relevant parts of the PHP manual.

5. get_browser()

Isn’t it good to see what the user’s browser can do before sending the page? get_browser() can get the user The browser type, and the functions supported by the browser, but first you need a php_browscap.ini file to serve as a reference file for the function.

It should be noted that this function’s judgment of browser functions is based on the general characteristics of this type of browser. For example, if the user has turned off JavaScript support in the browser, the function has no way of knowing this. However, this function is still very accurate in determining browser type and OS platform.

6. debug_print_backtrace()

This is a debugging function that can help you find logical errors in the code. To understand this function, let’s look at an example:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$a</span><span> = 0;   </span></span></li>
<li>
<span class="keyword">function</span><span> iterate() {   </span>
</li>
<li class="alt">
<span class="keyword">global</span><span> </span><span class="vars">$a</span><span>;   </span>
</li>
<li>
<span class="keyword">if</span><span>( </span><span class="vars">$a</span><span> < 10 )   </span></li><li class="alt"><span>recur();   </span></li><li><span class="func">echo</span><span> </span><span class="vars">$a</span><span> . &ldquo;, &ldquo;;   </span></li><li class="alt"><span>}   </span></li><li><span class="keyword">function</span><span> recur() {   </span></li><li class="alt"><span class="keyword">global</span><span> </span><span class="vars">$a</span><span>;   </span></li><li><span class="vars">$a</span><span>++;   </span></li><li class="alt"><span class="comment">// how did I get here?  </span><span> </span></li><li><span class="func">echo</span><span> &ldquo;nnn&rdquo;;   </span></li><li class="alt"><span>debug_print_backtrace();   </span></li><li><span class="keyword">if</span><span>( </span><span class="vars">$a</span><span> < 10 )   </span></li><li class="alt"><span>iterate();   </span></li><li><span>}   </span></li><li class="alt"><span>iterate();   </span></li><li><span># OUTPUT:   </span></li><li class="alt"><span>#0 recur() called at [C:htdocsphp_stuffindex.php:8]   </span></li><li><span>#1 iterate() called at [C:htdocsphp_stuffindex.php:25]   </span></li><li class="alt"><span>#0 recur() called at [C:htdocsphp_stuffindex.php:8]   </span></li><li><span>#1 iterate() called at [C:htdocsphp_stuffindex.php:21]   </span></li><li class="alt"><span>#2 recur() called at [C:htdocsphp_stuffindex.php:8]   </span></li><li><span>#3 iterate() called at [C:htdocsphp_stuffindex.php:25]   </span></li><li class="alt"><span>#0 recur() called at [C:htdocsphp_stuffindex.php:8]   </span></li><li><span>#1 iterate() called at [C:htdocsphp_stuffindex.php:21]   </span></li><li class="alt"><span>#2 recur() called at [C:htdocsphp_stuffindex.php:8]   </span></li><li><span>#3 iterate() called at [C:htdocsphp_stuffindex.php:21]   </span></li><li class="alt"><span>#4 recur() called at [C:htdocsphp_stuffindex.php:8]   </span></li><li><span>#5 iterate() called at [C:htdocsphp_stuffindex.php:25] </span></li></ol>

7. metaphone()

This function returns the metaphone value of the word. Words with the same pronunciation have the same metaphone value, which means that this function can help you determine whether the pronunciation of two words is the same.

8. natsort()

natsort() can arrange an array in a natural sorting method. Let’s take a look at an example:

<ol class="dp-c"><li class="alt"><span><span class="vars">$items</span><span> = </span><span class="keyword">array</span><span>(   </span></span></li><li><span>&ldquo;100 apples&rdquo;, &ldquo;5 apples&rdquo;, &ldquo;110 apples&rdquo;, &ldquo;55 apples&rdquo;   </span></li><li class="alt"><span>);   </span></li><li><span class="comment">// normal sorting:  </span><span> </span></li><li class="alt"><span>sort(</span><span class="vars">$items</span><span>);   </span></li><li><span>print_r(</span><span class="vars">$items</span><span>);   </span></li><li class="alt"><span># Outputs:   </span></li><li><span># Array   </span></li><li class="alt"><span># (   </span></li><li><span># [0] => 100 apples   </span>
</li>
<li class="alt"><span># [1] => 110 apples   </span></li>
<li><span># [2] => 5 apples   </span></li>
<li class="alt"><span># [3] => 55 apples   </span></li>
<li><span># )   </span></li>
<li class="alt">
<span>natsort(</span><span class="vars">$items</span><span>);   </span>
</li>
<li>
<span>print_r(</span><span class="vars">$items</span><span>);   </span>
</li>
<li class="alt"><span># Outputs:   </span></li>
<li><span># Array   </span></li>
<li class="alt"><span># (   </span></li>
<li><span># [2] => 5 apples   </span></li>
<li class="alt"><span># [3] => 55 apples   </span></li>
<li><span># [0] => 100 apples   </span></li>
<li class="alt"><span># [1] => 110 apples   </span></li>
<li><span># ) </span></li>
</ol>

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.

Let’s look at an example:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$dictionary</span><span> = </span><span class="keyword">array</span><span>(   </span></span></li>
<li><span>“php”, “javascript”, “css”   </span></li>
<li class="alt"><span>);   </span></li>
<li>
<span class="vars">$word</span><span> = “japhp”;   </span>
</li>
<li class="alt">
<span class="vars">$best_match</span><span> = </span><span class="vars">$dictionary</span><span>[0];   </span>
</li>
<li>
<span class="vars">$match_value</span><span> = levenshtein(</span><span class="vars">$dictionary</span><span>[0], </span><span class="vars">$word</span><span>);   </span>
</li>
<li class="alt">
<span class="keyword">foreach</span><span>(</span><span class="vars">$dictionary</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$w</span><span>) {   </span>
</li>
<li>
<span class="vars">$value</span><span> = levenshtein(</span><span class="vars">$word</span><span>, </span><span class="vars">$w</span><span>);   </span>
</li>
<li class="alt">
<span class="keyword">if</span><span>( </span><span class="vars">$value</span><span> < </span><span class="vars">$match_value</span><span> ) {   </span>
</li>
<li>
<span class="vars">$best_match</span><span> = </span><span class="vars">$w</span><span>;   </span>
</li>
<li class="alt">
<span class="vars">$match_value</span><span> = </span><span class="vars">$value</span><span>;   </span>
</li>
<li><span>}   </span></li>
<li class="alt"><span>}   </span></li>
<li>
<span class="func">echo</span><span> “Did you mean the ‘</span><span class="vars">$best_match</span><span>’ category?”; </span>
</li>
</ol>

10. glob()

glob () will make you feel stupid using opendir(), readdir() and closedir() to find files.

<ol class="dp-c">
<li class="alt"><span><span class="keyword">foreach</span><span> (</span><span class="func">glob</span><span>(“*.php”) </span><span class="keyword">as</span><span> </span><span class="vars">$file</span><span>)   </span></span></li>
<li>
<span class="func">echo</span><span> “</span><span class="vars">$file</span><span>n”; </span>
</li>
</ol>

I hope these 10 PHP functions introduced in this article can help you use PHP better.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445761.htmlTechArticleFunctions are the source of such power in PHP, but many PHP functions are not fully utilized. Here, we briefly introduce 10 uncommon but very useful functions. 1. sys...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn