8 required PHP function instance code

Source: Internet
Author: User
This article will share eight essential PHP functions, all of which are very useful: passing any number of function parameters and using glob () search for files, obtain memory usage information, obtain CPU usage information, and obtain system Constants

This article will share eight essential PHP functions, all of which are very useful: passing any number of function parameters and using glob () search for files, obtain memory usage information, obtain CPU usage information, and obtain system Constants

Programmers who have developed PHP should be clear that PHP has many built-in functions and can master them to help you better develop PHP, this article will share eight essential PHP functions, all of which are very practical and I hope all PHP developers can master them.
1. pass any number of function parameters
2. Use glob () to find files
3. Obtain memory usage information
4. Obtain CPU usage information
5. Obtain system Constants
6. Generate a unique id
7. serialization
8. String Compression

1. Transfer any number of function parameters. In. NET or JAVA programming, the number of function parameters is fixed, but PHP allows you to use any number of parameters. The following example shows the default parameters of the PHP function:

The Code is as follows:


// Functions with two default parameters
Function foo ($ arg1 = ", $ arg2 = "){
Echo "arg1: $ arg1 \ n ";
Echo "arg2: $ arg2 \ n ";
}
Foo ('hello', 'World ');
/* Output:
Arg1: hello
Arg2: world
*/
Foo ();
/* Output:
Arg1:
Arg2:
*/
The following example shows the variable parameter usage of PHP. It uses the func_get_args () method:
// Yes, the parameter list is empty
Function foo (){
// Obtain the array of all input parameters
$ Args = func_get_args ();
Foreach ($ args as $ k =>v v ){
Echo "arg". ($ k + 1). ": $ v \ n ";
}
}
Foo ();
/* Nothing will be output */
Foo ('hello ');
/* Output
Arg1: hello
*/
Foo ('hello', 'World', 'again ');
/* Output
Arg1: hello
Arg2: world
Arg3: again
*/

2. You can use glob () to find the function names of most PHP functions in a file. However, when you see glob, you may not know what this is for. In fact, glob () is the same as scandir () and can be used to find files. Please refer to the following usage:

The Code is as follows:


// 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:

The Code is as follows:


// Retrieve the PHP and TXT files
$ 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:

The Code is as follows:


$ Files = glob ('../images/a *. jpg ');
Print_r ($ files );
/* Output:
Array
(
[0] => ../images/apple.jpg
[1] => ../images/art.jpg
)
*/


If you want to obtain the absolute path, you can call the realpath () function:

The Code is as follows:


$ Files = glob ('../images/a *. 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. Obtain memory usage information the PHP memory recovery mechanism is very powerful. You can also use the PHP script to obtain the current memory usage and call the memory_get_usage () function to obtain the current memory usage, call the memory_get_peak_usage () function to obtain the peak memory usage. The reference code is as follows:

The Code is as follows:


Echo "Initial:". memory_get_usage (). "bytes \ n ";
/* Output
Initialize: 361400 bytes
*/
// Memory usage
For ($ I = 0; I I <100000; $ I ++ ){
$ Array [] = md5 ($ I );
}
// Delete half of the memory
For ($ I = 0; I I <100000; $ I ++ ){
Unset ($ array [$ I]);
}
Echo "Final:". memory_get_usage (). "bytes \ n ";
/* Prints
Final: 885912 bytes
*/
Echo "Peak:". memory_get_peak_usage (). "bytes \ n ";
/* Output peak value
Peak: 13687072 bytes
*/

4. Obtain the CPU usage information to obtain the memory usage, or use getrusage () of PHP to obtain the CPU usage. This method is unavailable in windows.

The Code is as follows:


Print_r (getrusage ());
/* Output
Array
(
[Ru_oublock] => 0
[Ru_inblock] => 0
[Ru_msgsnd] => 2
[Ru_msgrcv] => 3
[Ru_maxrss] = & gt; 12692
[Ru_ixrss] = & gt; 764
[Ru_idrss] = & gt; 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] = & gt; 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: maximum resident set size ru_ixrss: All shared memory size ru_idrss: all non-shared memory size ru_minflt: Page recycling ru_majflt: Page failure ru_nsignals: Received Signal ru_nvcsw: Active context switching ru_nivcsw: passive context switching ru_nswap: swap zone Duration: 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) to see how much CPU your script consumes, we need to look at the value of "user-mode time" and "System Kernel Time. Second and microsecond are provided separately. You can divide the microsecond value by 1 million and add it to the second value to obtain the number of seconds with a decimal part.

The Code is as follows:

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.