Introduction to nine Practical php functions

Source: Internet
Author: User
Tags glob
& Nbsp; even if you have been using PHP for many years, you may find some functions and functions you have never understood. Some of them are very useful, but they are not fully utilized. Not everyone will read the manual and function reference page by page from start to end! 1. functions with any number of parameters your SyntaxHig may accidentally discover some functions and functions you have never understood even if you have been using PHP for many years. Some of them are very useful, but they are not fully utilized. Not everyone will read the manual and function reference page by page from start to end!

1. functions with any number of parameters
You may already know that PHP allows defining functions with optional parameters. However, there are methods that fully allow any number of function parameters. The following is an example of an optional parameter:
Reference content is as follows:
// Function with 2 optional arguments
Function foo ($ arg1 =, $ arg2 = ){
Echo "arg1: $ arg1 ";
Echo "arg2: $ arg2 ";
}
Foo (hello, world );
/* Prints:
Arg1: hello
Arg2: world
*/
Foo ();
/* Prints:
Arg1:
Arg2:
*/
 
Now let's see how to create a function that can accept any number of parameters. This time, you need to use the func_get_args () function:
Reference content is as follows:
// 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 v ){
Echo "arg". ($ k 1). ": $ v ";
}

}
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 are familiar with it. You can think of it as a version that is more powerful than the scandir () function. you can search for files in a certain mode.

Reference content is as follows:
// 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 obtain multiple files like this:
Reference content is as follows:
// Get all php files AND txt files
$ Files = glob (*. {php, txt}, GLOB_BRACE );
Print_r ($ files );
/* Output looks like:
Array
(
[0] => phptest. php
[1] => pi. php
[2] => post_output.php
[3] => test. php
[4] => log.txt
[5] => test.txt
)
*/

Note that these files can actually return a path, depending on the query conditions:
Reference content is as follows:
$ Files = glob (../images/a *. jpg );
Print_r ($ files );
/* Output looks like:
Array
(
[0] => ../images/apple.jpg
[1] => ../images/art.jpg
)
*/
 
If you want to obtain the complete path of each file, you can call the realpath () function:
Reference content is as follows:
$ 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: wampwwwimagesapple.jpg
[1] => C: wampwwwimagesart.jpg
)
*/

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.