Even if you're using PHP for years, you'll stumble across functions and functions that you never knew about. Some of them are very useful, but not fully utilized. Not everyone will read the manual and function references from beginning to end.
1, any number of parameters of the function
As you may already know, PHP allows you to define functions for optional parameters. But there is also a way to fully allow any number of function parameters. The following are examples of optional parameters:
The following are the referenced contents: function with 2 optional arguments function foo ($arg 1 = ', $arg 2 = ') { echo "Arg1: $arg 1n"; echo "ARG2: $arg 2n"; } Foo (' Hello ', ' world '); /* Prints: Arg1:hello Arg2:world */ Foo (); /* Prints: Arg1: ARG2: */ |
Now let's look at how to create a function that accepts any number of arguments. This time you need to use the Func_get_args () function:
The following are the referenced contents: Yes, the argument list can be empty function foo () { Returns an array of all passed arguments $args = Func_get_args (); foreach ($args as $k => $v) { echo "Arg". ($k + 1). ": $vn"; } } Foo (); /* Prints Nothing * * Foo (' Hello '); /* Prints Arg1:hello */ Foo (' Hello ', ' world ', ' again '); /* Prints Arg1:hello Arg2:world Arg3:again */ |
2. Use Glob () to find files
Many PHP functions have long descriptive names. However, it may be difficult to say what the glob () function can do unless you have used it many times and become familiar with it. You can think of it as a more powerful version than the Scandir () function, and you can search for files in a pattern.
The following are the referenced contents: Get all PHP files $files = Glob (' *.php '); Print_r ($files); /* Output looks like: Array ( [0] => phptest.php [1] => pi.php [2] => post_output.php [3] => test.php ) */ |
You can get multiple files like this:
//Get a ll php files and txt files $files = Glob (' *.{ Php,txt} ', glob_brace); Print_r ($files); /* Output looks like: Array ( [0] => phptest.php [1] => P i.php [2] => post_output.php [3] => test.php [4] => log.txt [5] => test.txt */ |