Nine useful PHP Functions

Source: Internet
Author: User

1. Any number of parameters of the Function

You may know that PHP allows you to define a default parameter function. But you may not know that PHP also allows you to define a function with all arbitrary parameters.

The following example shows the default parameter functions:

// Functions with two default parameters

Function foo ($ arg1 =, $ arg2 = ){

Echo "arg1: $ arg1 ";

Echo "arg2: $ arg2 ";

}

Foo (hello, world );

/* Output:

Arg1: hello

Arg2: world

*/

Foo ();

/* Output:

Arg1:

Arg2:

*/

 

Now let's take a look at a function with an indefinite parameter. is it used? 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 ";

}

}

Foo ();

/* Nothing will be output */

Foo (hello );

/* Output

Arg1: hello

*/

Foo (hello, world, again );

/* Output

Arg1: hello

Arg2: world

Arg3: again

*/

 

2. Use Glob () to find files

Many PHP functions have a long self-explanatory function name? During glob (), you may not know what this function is for, 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 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:

$ 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, can you call it? Realpath () function:

$ 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: wampwwwimagesapple.jpg

[1] => C: wampwwwimagesart.jpg

)

*/

 

3. memory usage information

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 ";

/* 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 ";

/* Prints

Final: 885912 bytes

*/

Echo "Peak:". memory_get_peak_usage (). "bytes ";

/* Output peak value

Peak: 13687072 bytes

*/

 

4. CPU usage information

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] = & 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: 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)

 

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.