10 classic PHP functions

  • 2020-10-07 18:36:52
  • OfStack

1. sys_getloadavg()

sys_getloadavt() can obtain the system load. This function returns an array of 3 elements, each representing the system's average load over the next 1, 5, and 15 minutes.

Instead of letting the server go down under too much load, die actively drops a script when the system is under too much load. sys_getloadavg() is designed to help you do this. Unfortunately, this function is not valid under windows.

2. pack()

Pack() can convert 32-bit hexadecimal strings returned by md5() to 16-bit binary strings, saving storage space.

3. cal_days_in_month()

cal_days_in_month() returns the number of days in a specified month.

4. _()

WordPress developers often see this function, along with _e(). These two functions have the same function. When used in combination with gettext(), the website can be multilingual. For details, please refer to the relevant section of the PHP manual.

5. ge
owser()

Isn't it a good idea to see what the user's browser can do before sending a page? ge
owser() gets the user's browser type and what the browser supports, but first you need an ES44en_browscap.ini file to refer to functions.

Note that this function makes a judgment about browser functionality based on 1 general feature of the browser. For example, if the user turns off browser support for JavaScript, the function does not know this point. But in terms of determining the browser type and the OS platform, the function is pretty accurate.

6. debug_print_backtrace()

This is a debugging function that helps you find logic errors in your code. To understand this function, let's look at an example:

$a = 0;
function iterate() {
global $a;
if( $a < 10 )
recur();
", ";
}
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:\htdocs\php_stuff\index.php:25]

7. metaphone()

This function returns the metaphone value of a word, and the same metaphone value for a word that sounds the same, meaning it will help you determine if two words sound the same. But not for Chinese...

8. natsort()

natsort() can arrange an array in a natural sort.

$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
# )

9. levenshtein()

Levenshtein() tells you the "distance" between two words. It tells you how many letters you need to insert, replace, and delete if you want to change one word into another.

Take an example:

$dictionary = array(
"php", "javascript", "css"
);
$word = "japhp";
$best_match = $dictionary[0];
$match_value = levenshtein($dictionary[0], $word);
foreach($dictionary as $w) {
$value = levenshtein($word, $w);
if( $value < $match_value ) {
$best_match = $w;
$match_value = $value;
}
}
Did you the '$best_match' category? ;

10. glob()

glob() makes you feel silly looking for files using opendir(), readdir() and closedir().

foreach (glob (" *. php ") as $file)
echo "$file \ n";


Related articles: