PHP functions arbitrary number of parameters and find file instances in detail

Source: Internet
Author: User
Tags glob
1. Any number of arguments to the PHP function

You probably know that PHP allows you to define a default parameter for a function. But you may not know that PHP also allows you to define a function that is completely arbitrary.
The following is an example of a function that shows you the default parameters:

Two default parameters function foo ($arg 1 = ', $arg 2 = ') {echo "arg1: $arg 1\n"; echo "arg2: $arg 2\n";} Foo (' Hello ', ' world ');/* Output: Arg1:helloarg2:world*/foo ();/* Output: arg1:arg2:*/

Now let's take a look at a function with an indeterminate parameter, which uses the Func_get_args () method:

Yes, the formal parameter list is empty function foo () {//Gets all the array of incoming parameters $args = Func_get_args (); foreach ($args as $k = + $v) {echo "arg". $k + 1). ": $v \ n";}} Foo ();/* Nothing will output */foo (' hello ');/* Output arg1:hello*/foo (' Hello ', ' world ', ' again ');/* Output arg1:helloarg2:worldarg3:again* /

2. Glob () Find Files
There are a lot of PHP functions that have a long self-explanatory function name, but when you see Glob (), you probably don't know what the function is for, unless you're already familiar with it.
You can think of this function as good? Scandir (), which can be used to find files.

Get all the suffixes for php files $files = glob (' *.php ');p rint_r ($files);/* Output: Array ([0] = = phptest.php[1] + pi.php[2] = Post_ OUTPUT.PHP[3] = test.php) */

You can also find multiple suffix names

Fetch PHP files and txt files $files = glob (' *.{ Php,txt} ', Glob_brace);p Rint_r ($files);/* Output: Array ([0] = phptest.php[1] + pi.php[2] = post_output.php[3] =& Gt TEST.PHP[4] = log.txt[5] = test.txt) */

You can also add a path:

$files = Glob ('.. /images/a*.jpg ');p rint_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);p Rint_r ($files);/* Output looks like:array ([0] = c:\wamp\www\images\apple.jpg[1] = C:\wamp\www\images\art.jpg) */
Related Article

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.