Some neglected PHP functions)

Source: Internet
Author: User
Tags glob
: This article mainly introduces some neglected PHP functions. For more information about PHP tutorials, see. From: http://www.gracecode.com/posts/3013.html

I really don't need to know. In fact, there are still many good things that we are familiar with in PHP. When I saw this article, I burst into tears several times. I strongly recommend that you sort it out by myself.

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. There is a sample code in this document, so you can see its purpose.

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 code

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 );}}

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 do this directly.

$ Pass_hash = md5 ("my-password", true); // PHP 5 +

One of the advantages of doing so is that it can reduce the string storage space (how much can it save? It may be another article ).

Here is a sample code that can pack arrays

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 promise you have implemented functions similar to this: ^)

_()

Er, this is indeed a PHP function (or the shortest PHP built-in function ). _()Is its nickname. Its name is Gettext ().

Anyone who has written Wordpress skins will understand __()And _ E ()In fact, PHP already comes with related functions.

// Set language to Germansetlocale (LC_ALL, 'de _ de'); // Specify location of translation tablesbindtextdomain ("myPHPApp ",". /locale "); // Choose domaintextdomain (" 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 it yourself. $ _ SERVER ['http _ USER_AGENT ']The 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 "it hurts". 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 the function ):

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] => 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 #)

For more information about natural sorting algorithm rules, see the documentation here.

Glob ()

The functions of this function are also Tearful. without mentioning the functions, we can directly use the sample code.

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 only list directories.

PHP Filter

If you are still performing regular expression verification on the string, 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 ));

Because it is a new module, there are still 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.


The above section introduces some neglected PHP functions (sorting), including some content, and hopes to help those who are interested in PHP tutorials.

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.