When we program in PHP, often we limit ourselves to a limited number of API functions: those common, like print (), header (), define (), isset (), htmlspecialchars (), etc. If there are some features needed, often we write new by making use of these basic components that we know. The API for PHP really offer a lot of features, some useless and some useful, that are rarely used. I searched the features available and I am interested in finding someone that I really need. Here, I share with you my discoveries.
1. sys_getloadavg ()
sys_getloadvg () is a function that returns three measures “load” its a system. The load is the number of processes in run queue system. In 3 elements of the matrix are the average load for the last 1, 5 and 15 minutes.
1
2
3
4
5
| $load = sys_getloadavg(),
the ($load[0] > 80) {
header('HTTP/1.1 503 Too busy, try again later ');
the('The server is busy later. Please try again '.);
} |
Rather than have the Web service is no longer available (offline) simply because everything fell apart after too many requests received, This code will proactively to accept some requests and denied others. But this code does not work on Windows.
2. cal_days_in_month ()
cal_days_in_month () returns the number of days in a given month:
1
2
| $days = cal_days_in_month(CAL_GREGORIAN, date("m"), date("And")); / / 31
echo($days - data("D") + 1 )."Days until".date("F", mktime(0, 0, 0, data("m") + 1, 1, 1970)); |
3. get_browser ()
Would not it be nice to discover what can make a user's browser before sending the page? Well, you can with get_browser (). You will need to first php_browscap.ini, browscap directive and point to the file. You could have something like this:
1
2
3
| $browser = get_browser(null, true);
if(!$browser["frames"] | | !$ browser[Cookies"])
echo "Please update your browser or use a more modern browser and have cookies enabled because some sections of this site may be inaccessible"; |
This is not able to detect individual configurations of browser, However,, can not be used to detect if JavaScript is enabled, for example. This feature can be useful to know which browser you use and which platform is used.
4. debug_print_backtrace ()
It can be very difficult to track down a bug in the code, in particular when looking for a logic error, but thanks to debug_print_backtrace() would be much easier. Such as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| $a = 0;
function iterate()
{
global $a;
if( $a < 10 ) recur();
echo $a.", ";
}
function recur()
{
global $a;
$a++;
// how did I get here?
echo " \n \n \n ";
debug_print_backtrace();
if ( $a < 10 ) iterate ();
}
iterate ();
# OUTPUT: #0 recur() called at [C:\htdocs\php_stuff\index.php:8] #1 iterate() called at [C:\htdocs\php_stuff\index.php:25] #0 recur() called at [C:\htdocs\php_stuff\index.php:8] #1 iterate() called at [C:\htdocs\php_stuff\index.php:21] #2 recur() called at [C:\htdocs\php_stuff\index.php:8] #3 iterate() called at [C:\htdocs\php_stuff\index.php:25] #0 recur() called at [C:\htdocs\php_stuff\index.php:8] #1 iterate() called at [C:\htdocs\php_stuff\index.php:21] #2 recur() called at [C:\htdocs\php_stuff\index.php:8] #3 iterate() called at [C:\htdocs\php_stuff\index.php:21] #4 recur() called at [C:\htdocs\php_stuff\index.php:8] #5 iterate() called at [C:\htdocsphp_stuffindex.php:25] [ ... ] |
5. natsort()
natsort() is a function that sorts an array of elements in a natural way (ie, in an order that seems logical to a person), rather than a kind of alphabetical order. Take, for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| $items = array("100 apples", "5 apples", "110 apples", "55 apples");
/ / normal order:
black($items);
print_r($items);
# The result:
Array #
# (
# [0] => 100 apples
# [1] => 110 apples
# [2] => 5 apples
# [3] => 55 apples
#)
natsort($items);
print_r($items);
# The result:
# Array
# (
# [2] => 5 apples
# [3] => 55 apples
# [0] => 100 apples
# [1] => 110 apples
#) |
6. Levenshtein()
Levenshtein() is a feature that helps you find the best similarity between 2 words. Let, for example, the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| $dictionary = array( "Php" , "Javascript" , "Css" );
$word = "japhp";
$best_match = $dictionary[0];
$match_value = levenshtein ($dictionary[0] , $word);
foreach($dictionary as $in)
{
$value = levenshtein($word , $in);
if( $value < $match_value)
{
$best_match = $in;
$match_value = $value;
}
}
echo "Did you mean the category ' $best_match ' ?"; |
In this case, the user was asked to provide a category name. Wrote “japhp”, that is not valid. Since this is likely to be a typo, With this feature we can find the category most similar (“Did you mean category ‘php’?”).
7. globe()
globe() , will make you feel stupid after using opendir(), readdir() e closedir() look for a file. The procedure is simple:
1
2
| foreach (globe("*. php") as $file)
echo "$ file n"; |
More?
There are a lot of useful functions and unknown out there as http_build_query(), register_shutdown_function() e pspell_suggest, what are your favorite?
Source: infinity-infinity.com
Recent Comments