Some of the neglected PHP functions (collation)

Source: Internet
Author: User
Tags glob
From: http://www.gracecode.com/posts/3013.html

Really do not have to know, in fact, we are familiar with PHP has a lot of good things have not been explored. See this article, then tears ran several times, the key recommendation, by the way I also do a collation.

Sys_getloadavg ()

This function returns the load mean information for the current system (not applicable under Windows), and the detailed documentation can be read through the relevant PHP documentation. There is a sample code in the document that basically shows its purpose.

{Header (' http/1.1 503 Too Busy, try again later '), Die (' Server Too busy. Please try again later. ');}

PS, if "Unfortunately" your PHP environment does not have this function, you can consider using the following code via

if (!function_exists (' Sys_getloadavg ')) {function sys_getloadavg () {$loadavg _file = '/proc/loadavg '; if (file_exists ($ Loadavg_file) {return explode (CHR), file_get_contents ($loadavg _file));} return Array (0,0,0); }}

This feature, if used properly, can relieve some of the pressure on the server.

Pack ()

The pack also has a function for unpack, which is used to compress the binary string, and the author's example is very clear

$pass _hash = Pack ("h*", MD5 ("My-password"));

If you use PHP5, you can do this directly

$pass _hash = MD5 ("My-password", true); PHP 5+

One of the benefits of this is that you can reduce the number of storage spaces (how much can you save?). It could be another article).

Here's another example code that can be a pack array via

Cal_days_in_month ()

The function can 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 that you have implemented a function like this: ^)

_()

Well, it's also a PHP function (and possibly the shortest PHP built-in function). _ () is its "nickname", its name is GetText ().

A friend who has written a Wordpress skin will understand the functions of __ () and _e (), in fact, PHP already has its own function.

Set language to Germansetlocale (Lc_all, ' de_de '); Specify location of translation Tablesbindtextdomain ("Myphpapp", "./locale"); Choose Domaintextdomain ("Myphpapp"); echo _ ("Has a nice Day");

With GetText you can write multi-lingual applications, and now you may be interested in writing locale files, which is not the focus of this article, more information can be moved here.

Get_browser ()

Frankly, when I saw this function, I was completely in tears. With this function, it is no longer necessary to analyze the string $_server[' Http_user_agent '.

For more information, refer to here. Before using this function, you may need a Browscap.ini configuration file that you can take care of.

Debug_print_backtrace ()

Before looking at the function call stack, I will use extensions such as xdebug, in fact, the PHP5 version has built-in related functions.

By the way, share a "egg pain" trick, if you can't remember the name of the function, you can use this code to achieve the same purpose (it seems to remember that function):

Gettraceasstring ()));

Natsort ()

This function is used for natural sorting, which you might want to use. Post relevant documentation links and sample code

$items = Array ("Apples", "5 apples", "apples", "apples"); Normal Sorting:sort ($items);p rint_r ($items); # Outputs: # Array # (# [0] = + Apples # [1] = apples # [2] = 5 Apples # [3] = apples #) Natsort ($items);p rint_r ($items); # Outputs: # Array # (# [2] = 5 Apples # [3] = apples # [0] = + Apples # [1] = + Apples #)

For the algorithm rules for natural sorting, refer to the documentation here.

Glob ()

The functions of this function also make people feel tears, first not to say function directly on the sample code

foreach (Glob ("*.php") as $file) {echo "$file \ n";}

Compared to what you already know about the purpose of the function, we can have more "gameplay", such as the display directory (via):

$dirs = Array_filter (Glob ($path. ' * '), ' is_dir ');

Of course, file recursion you can also consider using the lower SPL extension.

Supplement by Immortals:

Glob has a parameter option Glob_onlydir can only list directories

PHP Filter

If you're still validating the string, then it's really "out". Since the PHP5.2 version, the built-in PHP fliter module is used to specifically verify the legality of e-mail, URLs, etc., sample code:

Var_dump (Filter_var (' bob@example.com ', filter_validate_email));

Because it is a new module, there are many pitfalls, such as

Filter_var (' abc ', Filter_validate_boolean); BOOL (false) Filter_var (' 0 ', Filter_validate_boolean); BOOL (false)

But this does not affect us to try. For more information about PHP Filter, I believe I can carry it out and write another article.

Finally, I sigh that PHP is actually a timeless tool, and we're going to have to repeat the tragic creation of a wheel. Therefore, always look at the PHP document every time there will be a new harvest.


Above introduces some of the neglected PHP functions (collation), including aspects of the content, you want to be interested in the PHP tutorial friends helpful.

  • Related Article

    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.