1. Any number of parameters of the function
You may know that PHP allows you to define a function of 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:
Two functions for default parameters function 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 formal parameter list is empty function foo () { Gets the array of all incoming arguments $args = Func_get_args (); foreach ($args as $k => $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. Use Glob () to find files
Many PHP functions have a long, self explanatory function name, but when you see Glob (), you may not know what the function is for, unless you are 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 files $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 files 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 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. Memory usage Information
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 */ Using memory for ($i = 0; $i < 100000; $i + +) { $array []= MD5 ($i); } Remove 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 \ n"; /* Output Peak peak:13687072 bytes */ |
4. CPU Usage Information
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] => 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 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) |