Recently wrote a few PHP practical functions sorted out, made into a document, write instructions, easy to use later! A total of 4 PHP practical functions, now share with you, like friends can put it up, maybe later use.
1. php function of any number of parameters
you may know that PHP allows you to define a function with a default parameter. But you may not know that PHP also allows you to define a function of an entirely arbitrary parameter
Here is an example of a function that shows you the default parameters:
function of two default parameters
foo ($arg 1 = ', $arg 2 = ') {
echo "arg1: $arg 1\n";
echo "ARG2: $arg 2\n";
}
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, which uses the Func_get_args () method:
Yes, the parameter list is an empty
function foo () {
//Gets an array of all incoming arguments
$args = Func_get_args ();
foreach ($args as $k => $v) {
echo "arg". ( $k + 1). ": $v \ n";
}
}
Foo ();
* * What will not output
/foo (' Hello ');
/*
output
Arg1:hello
/foo (' Hello ', ' world ', ' again ');
/* Output
Arg1:hello
arg2:world
arg3:again
* *
2. Glob () Find Files
There are a lot of PHP functions that have a long, self-contained function name, but when you see Glob (), you may not know what this function is for, unless you're already familiar with it.
You can think of this function as good? Scandir (), it can be used to find files.
Get all the suffixes for php file
$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 suffix names
Take 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 a path:
$files = Glob ('.. /images/a*.jpg ');
Print_r ($files);
/* Output:
Array
(
[0] => ... /images/apple.jpg
[1] =>. /images/art.jpg
)
* * *
If you want an absolute path, you can call the? 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:\wamp\www\images\apple.jpg
[1] => C:\wamp\www\images\ Art.jpg
)
* * *
3. View Memory usage
observing the memory usage of your program allows you to better optimize your code.
PHP is a garbage collection mechanism, and has a very complex memory management mechanism. You can know the memory used by your script. To know the current memory usage, you can use the Memory_get_usage () function, and if you want to know the peak value of memory use, you can call the Memory_get_peak_usage () function.
echo "Initial:". Memory_get_usage (). "Bytes \ n";
/* Output
initial:361400 bytes
*/
/Use Memory 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";
/* Prints
final:885912
bytes
/echo "Peak:". Memory_get_peak_usage (). "Bytes";
/* Output Peak
peak:13687072 bytes
* *
4. View CPU Usage
Use the Getrusage () function to let you know the CPU usage. Note that this feature is not available under 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] =>
[Ru_majflt] => 0
[Ru_nsignals] = > 1
[RU_NVCSW] =>
[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 is very obscure, unless you know the CPU well. Here are some explanations:
Ru_oublock: Block output operation
Ru_inblock: Block input operation
RU_MSGSND: Message Sent
RU_MSGRCV: Message Received
Ru_maxrss: Maximum resident set size
RU_IXRSS: Total Shared memory size
Ru_idrss: All non-shared memory sizes
Ru_minflt: Page Recycling
Ru_majflt: Page Expiration
Ru_nsignals: The signal received
RU_NVCSW: Active Context switching
RU_NIVCSW: Passive Context switching
Ru_nswap: Swap Area
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 to share to everyone of the PHP practical functions, I hope to help you learn.