9 Useful PHP functions and functions that must be known

Source: Internet
Author: User
Tags glob

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

*/

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.