This article mainly introduces four very practical PHP functions. For more information, see
This article mainly introduces four very practical PHP functions. For more information, see
I have recently compiled several practical PHP functions, compiled them into a document, and written instructions to facilitate future use! There are a total of four practical PHP functions. I would like to share with you that my favorite friends can add them to favorites, which may be useful in the future.
1. Any number of PHP function parameters
You may know that PHP allows you to define a default parameter function. However, you may not know that PHP allows you to define a function with all arbitrary parameters.
The following example shows the default parameter functions:
// Function foo ($ arg1 = '', $ arg2 ='') {echo "arg1: $ arg1 \ n"; echo "arg2: $ arg2 \ n ";} foo ('helloarg2, 'World');/* output: arg1: helloarg2: world */foo ();/* output: arg1: arg2 :*/
Now let's take a look at a function with an indefinite parameter. It uses the func_get_args () method:
// Yes, the parameter list is empty function foo () {// get all input parameter arrays $ args = func_get_args (); foreach ($ args as $ k => $ v) {echo "arg ". ($ k + 1 ). ": $ v \ n" ;}} foo ();/* No output */foo ('hello');/* output arg1: hello */foo ('hello', 'World', 'again ');/* output arg1: helloarg2: worldarg3: again */
2. Glob () Search for files
Many PHP functions have a long self-explanatory function name. However, when you see glob (), you may not know what this function is, unless you are familiar with it.
Do you think this function is good? Like scandir (), scandir can be used to find files.
// Obtain all files suffixed with PHP $ files = glob ('*. php '); print_r ($ files);/* output: Array ([0] => phptest. php [1] => pi. php [2] => post_output.php [3] => test. php )*/
You can also find multiple extension names
// Retrieve the PHP file and TXT file $ files = glob ('*. {php, txt} ', GLOB_BRACE); print_r ($ files);/* output: Array ([0] => phptest. php [1] => pi. php [2] => post_output.php [3] => test. php [4] => log.txt [5] => test.txt )*/
You can also add the following path:
$ Files = glob ('.. /images/*. jpg '); print_r ($ files);/* output: Array ([0] => .. /images/apple.jpg [1] => .. /images/art.jpg )*/
If you want to obtain the absolute path, can you call it? Realpath () function:
$ Files = glob ('.. /images/*. jpg '); // applies the function to each array element $ files = array_map ('realpath', $ files); print_r ($ files);/* output looks like: array ([0] => C: \ wamp \ www \ images \ apple.jpg [1] => C: \ wamp \ www \ images \ art.jpg )*/
3. view memory usage
Observe that the memory usage of your program allows you to better optimize your code.
PHP has a garbage collection mechanism and a complicated memory management mechanism. You can know the memory usage of your script. Do you want to know the current memory usage? Memory_get_usage () function. If you want to know the peak memory usage, you can call the memory_get_peak_usage () function.
Echo "Initial :". memory_get_usage (). "bytes \ n";/* output Initial: 361400 bytes * // memory usage for ($ I = 0; $ I <100000; $ I ++) {$ array [] = md5 ($ I) ;}// delete half of the memory for ($ I = 0; $ I <100000; $ I ++) {unset ($ array [$ I]);} echo "Final :". memory_get_usage (). "bytes \ n";/* printsFinal: 885912 bytes */echo "Peak :". memory_get_peak_usage (). "bytes \ n";/* output Peak: 13687072 bytes */
4. View CPU usage
Use? The getrusage () function allows you to know the CPU usage. Note that this function is not available in Windows.
Print_r (getrusage ()); /* output Array ([ru_oublock] => 0 [ru_inblock] => 0 [ru_msgsnd] => 2 [ru_msgrcv] => 3 [ru_maxrss] => 12692 [ru_ixrss] => 764 [ru_idrss] => 3864 [ru_minflt] => 94 [ru_majflt] => 0 [ru_nsignals] => 1 [ru_nvcsw] => 67 [ru_nivcsw] => 4 [ru_nswap => 0 [ru_utime. TV _usec] => 0 [ru_utime. TV _sec] => 0 [ru_stime. TV _usec] => 6269 [ru_stime. TV _sec] => 0) */
This structure seems obscure unless you know the CPU. The following are some explanations:
Ru_oublock: block output operation
Ru_inblock: block input operation
Ru_msgsnd: sent message
Ru_msgrcv: Received message
Ru_maxrss: the largest and smallest resident set
Ru_ixrss: All shared memory size
Ru_idrss: all non-shared memory sizes
Ru_minflt: Page recycling
Ru_majflt: the page is invalid.
Ru_nsignals: Received Signal
Ru_nvcsw: Active context switching
Ru_nivcsw: passive context switching
Ru_nswap: swap Zone
Ru_utime. TV _usec: User State time (microseconds)
Ru_utime. TV _sec: User State time (seconds)
Ru_stime. TV _usec: System Kernel Time (microseconds)
Ru_stime. TV _sec: System Kernel Time? (Seconds)
The above is the practical PHP functions shared with you, hoping to help you learn.