Some neglected PHP functions (simple arrangement)

Source: Internet
Author: User
Tags glob

Sys_getloadavg ()
This function returns the load mean information of the current system (of course not applicable to Windows). For more information, see related documents of PHP. Example in the document Code , Basically, we can see its purpose. Copy code The Code is as follows: <? PHP
$ Load = sys_getloadavg ();
If ($ load [0]> 80 ){
Header ('HTTP/1.1 503 too busy, try again later ');
Die ('server too busy. Please try again later .');
}

PS. If "Unfortunately" you do not have this function in your PHP environment, consider using the following codeCopy codeThe Code is as follows: if (! Function_exists ('sys _ getloadavg ')){
Function sys_getloadavg ()
{
$ Loadavg_file = '/proc/loadavg ';
If (file_exists ($ loadavg_file )){
Return explode (CHR (32), file_get_contents ($ loadavg_file ));
}
Return array (0, 0, 0 );
}
}

This feature can reduce part of the server pressure if used properly.

Pack ()
Another function corresponding to pack is unpack, which is used to compress binary strings. The author's example in this article is very clear.

$ Pass_hash = pack ("H *", MD5 ("My-Password"); If you use PhP5, you can directly

$ Pass_hash = MD5 ("My-Password", true); // One of the advantages of PHP 5 + is that it can reduce the string storage space (How much can it save? It may be another article.Article).

Here is a sample code that can pack arraysCopy codeThe Code is as follows: <? PHP
Function pack_array ($ V, $ ){
Return call_user_func_array (pack, array_merge (Array ($ v), (array) $ ));
}

Cal_days_in_month ()

This function can directly return the number of days in a specified month, for example

$ Days = cal_days_in_month (cal_gregorian, date ("M"), date ("Y"); // 31 I can assure you have implemented functions similar to this: ^)

_()
Er, this is indeed a PHP function (or the shortest PHP built-in function ). _ () Is its "Small name" and its name is gettext ().

Friends who have written WordPress skins will learn about the functions _ () and _ E (). In fact, PHP already comes with related functions.Copy codeThe Code is as follows: // set language to German
Setlocale (lc_all, 'de _ de ');

// Specify location of translation tables
Bindtextdomain ("myphpapp", "./locale ");

// Choose domain
Textdomain ("myphpapp ");

Echo _ ("Have a nice day ");

You can use gettext To Write multi-language applications. Now you may be interested in writing locale files. This is not the focus of this Article. For more information, see here.

Get_browser ()
Frankly speaking, I ran into tears when I saw this function. With this function, you no longer need to analyze the $ _ server ['HTTP _ user_agent '] string.

For more information, refer to here. Before using this function, you may need a browscap. ini configuration file. I believe you can do it.

Debug_print_backtrace ()
I used to view the function call stack. I will use extensions such as xdebug. In fact, related functions have been built into PhP5 later.

By the way, I would like to share some tips on "egg". If you cannot remember the name of this function, you can use this code to achieve the same purpose (it seems that you still remember that function is reliable ):Copy codeThe Code is as follows: <? PHP
$ E = new exception ();
Print_r (str_replace ('/path/to/code/', '', $ e-> gettraceasstring (); natsort ()
This function is used for natural sorting, which may be used by everyone. Post relevant document links and sample code

$ 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
#) For natural sortingAlgorithmRules, refer to the documentation here.

Glob ()
The functions of this function are also tearful. without mentioning the functions, we can directly use the sample code.Copy codeThe Code is as follows: foreach (glob ("*. php") as $ file ){
Echo "$ file \ n ";
}

As you know the purpose of this function, we can have more "gameplay", such as displaying the directory ():

$ Dirs = array_filter (glob ($ path. '*'), 'is _ dir'); of course, you can also consider using the SPL extension for file recursion.

Supplement by God:

Glob has a parameter option glob_onlydir, which can be used to list only the Directory PHP filter.
If you are still verifying the string in regular expressions, it will be "out. After php5.2, the PHP fliter module has been built in to verify the validity of emails and URLs. Sample Code:

Var_dump (filter_var ('Bob @ example.com ', filter_validate_email); as a new module, there are many traps, such

Filter_var ('abc', filter_validate_boolean); // bool (false)
Filter_var ('0', filter_validate_boolean); // bool (false), but this does not affect our attempt. For more information about PhP filter, I believe I can write another article.

-- Split --

Finally, I lamented that PHP is actually a new tool that has never been used for a long time. Accidentally, we will have to create another wheel. Therefore, you should always look at the PHP documentation and get new results each time.

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.