1, pass any number of function parameters
We're in. NET or Java programming, the number of general function parameters is fixed, but PHP allows you to use any number of parameters. The following example shows you the default parameters for PHP functions:
// two functions with default parameters function foo ($arg 1$arg 2 =echo$arg 1\ n "; Echo $arg 2 \ n ";} Foo (' Hello ', ' World'); /* Output: Arg1:hello Arg2:world */foo (); /* Output: arg1:arg2: */
The following example is an indeterminate parameter usage of PHP, which is used in the? Func_get_args () Method:
//Yes, the formal parameter list is emptyfunctionfoo () {//gets the array of all incoming arguments$args=Func_get_args(); foreach($args as $k=$v) { Echo"Arg". ($k+1). ":$v\ n ";} } foo (); /*Nothing's going to go out.*/foo (' Hello ');/*Output Arg1:hello*/foo (' Hello ', ' World ',' again '); /*output Arg1:hello Arg2:world arg3:again*/
2. Use Glob () to find files
The function names of most PHP functions are literally understandable, but when you see the Glob (), you may not know what this is for, in fact Glob () and Scandir () can be used to find files, see the following usage:
// get all the files that are suffixed with PHP $files Glob (‘*. print_r($files/** *
3. Get Memory usage information
PHP's memory recovery mechanism is very powerful, you can also use PHP script to get the current memory usage, call the Memory_get_usage () function to get memory usage in the period, call the Memory_get_peak_usage () function to get the peak memory usage. The reference code is as follows:
Echo"Initial:". Memory_get_usage ()."bytes \ n";/*output initial:361400 bytes*/ //using 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 \ n";/*output Peak peak:13687072 bytes*/
4. Get System Constants
PHP provides very useful system constants that allow you to get the current line number (__line__), file (__file__), directory (__dir__), function name (__function__), class name (__class__), method name (__method__) and namespace (__namespace__), very much like C language.
We can assume that these things are mainly used for debugging, and when not necessarily, for example, can we use other files when we include them? __file__ (of course, you can also use __dir__ after PHP 5.3), here's an example.
//This was relative to the loaded script ' s path//It could cause problems when running scripts from different directories require_once(' Config/database.php '); //This is all relative to the this file's path//no matter where it was included from require_once(dirname(__file__) . '/config/database.php ');//some code//...My_debug ("Some debug message",__line__); /*Output line 4:some debug Message*/ //some more code//...My_debug ("Another debug message",__line__); /*Output line 11:another debug Message*/ functionMy_debug ($msg,$line) { Echo"Line$line:$msg\ n ";}
5. Generate a unique ID
Many friends use MD5 () to generate unique numbers, but MD5 () has several drawbacks: 1, unordered, which results in degraded sorting performance in the database. 2, too long, need more storage space. In fact, PHP comes with a function to generate a unique ID, this function is Uniqid (). Here's how to use:
// Generate unique String Echo uniqid / **/ / generate another unique stringecho Uniqid/** *
6. Serialization
How to serialize to JSON format, rest assured, PHP is ready for you, using PHP 5.2 or later users can use the Json_encode () and Json_decode () function to implement the JSON format serialization, the code is as follows:
//a complex array $myvar=Array(' Hello ', 42,Array(1, ' both '),' Apple '); //convert to a string $string= Json_encode ($myvar); Echo $string; /*prints ["Hello", 42,[1, "I"], "apple"]*/ //You can reproduce the original variable $newvar= Json_decode ($string); Print_r($newvar); /*prints Array ([0] = hello [1] = [2] = = Array ([0] = 1 [1] +/-) [3] = apple)*/
Reprinted from: Qi ba jiu 0 blog
PHP common functions and functions