Some overlooked PHP functions (simple collation) _php tips

Source: Internet
Author: User
Tags documentation gettext glob locale md5
Sys_getloadavg ()
This function returns the load mean information of the current system (not applicable under Windows), and the detailed documentation can be read through PHP documentation. There's a snippet of sample code in the document that basically shows what it's used for.
Copy Code code 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, you may consider using the following code via
Copy Code code as follows:

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 reduce some of the pressure on the server.

Pack ()
The pack corresponds to a function of unpack, which is used to compress binary strings, and 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); One of the benefits of PHP 5+ is the ability to reduce string storage (how much can be saved?) It might be another article again).

Here's another example code that can pack arrays via
Copy Code code as follows:

<?php
function Pack_array ($v, $a) {
Return Call_user_func_array (Pack,array_merge (Array ($v), (array) $a));
}

Cal_days_in_month ()

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

$days = Cal_days_in_month (Cal_gregorian, Date ("M"), Date ("Y")); 31 I can assure you that you have implemented functions of similar functions: ^)

_()
Well, it's also a PHP function (and probably 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 (), and PHP already has its own related functions.
Copy Code code 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");

Using GetText to write multi-language applications, you may now be interested in how to write locale files, but this is not the focus of this article, more information can be moved here.

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

More information can be consulted here. Before using this function, you may need a Browscap.ini configuration file that you can take care of.

debug_print_backtrace ()
Before viewing the function call stack, I would use extensions such as xdebug, in fact, the PHP5 version has been built into the relevant functions.

By the way, share an "egg ache" trick, and if you can't remember the name of the function, you can use this code to do the same (it seems to remember the function):
Copy Code code as follows:

<?php
$e = new Exception ();
Print_r (Str_replace ('/path/to/code/', ', ', $e->gettraceasstring ()); Natsort ()
This function is used for natural ordering, which you might want to use. Post related document links and sample code

$items = Array ("Apples", "5 apples", "apples", "apples");

Normal sorting:
Sort ($items);
Print_r ($items);
# Outputs:
# Array
# (
# [0] => apples
# [1] => apples
# [2] => 5 apples
# [3] => apples
# )

Natsort ($items);
Print_r ($items);
# Outputs:
# Array
# (
# [2] => 5 apples
# [3] => apples
# [0] => apples
# [1] => apples
# The algorithm rules for natural sorting can refer to the documentation here.

Glob ()
This function also makes people feel tears running, do not say the function directly on the sample code
Copy Code code as follows:

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

As you know the purpose of the function, we can have more "play", for example, on the display directory (via):

$dirs = Array_filter (Glob ($path. ') * '), ' is_dir '; Of course, file recursion you can also consider using the SPL extension.

Supplemented by Immortals:

Glob has a parameter option Glob_onlydir can only list directory PHP Filter
If you're still validating strings, then it's really "out." Since the PHP5.2 version, the built-in PHP fliter module is used to specifically verify the legality of email, URL, and so on, sample code:

Var_dump (Filter_var (' bob@example.com ', filter_validate_email)); because it is a newborn module, there are many pitfalls, such as

Filter_var (' abc ', Filter_validate_boolean); BOOL (FALSE)
Filter_var (' 0 ', Filter_validate_boolean); BOOL (false) but that doesn't affect the way we try. For more information on PHP Filter, I believe I can carry out another article.

--Split--

Finally, the exclamation of PHP is actually an enduring new tool, accidentally we will be tragic to repeat the creation of a wheel. Therefore, always look at the PHP document will have a new harvest every 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.