PHP function description _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP functions. The real power of PHP comes from its functions, but some PHP functions are not fully utilized, and not everyone will read the manual and function reference page by page from start to end, the real power of PHP comes from its functions, but some PHP functions are not fully utilized, and not everyone will read the manual and function reference page by page from start to end, here we will introduce you to these practical functions and functions.

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:
// Functionwith2optionalarguments
Functionfoo ($ arg1 = ", $ arg2 = "){
Echo "arg1: $ arg1 ";
Echo "arg2: $ arg2 ";
}

Foo ('hello', world ');
/* Prints:
Arg1: hello
Arg2: world
*/

Foo ();
/* Prints:
Arg1:
20. 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, theargumentlistcanbeempty
Functionfoo (){
// Returnsanarrayofallpassedarguments
$ Args = func_get_args ();
Foreach ($ argsas $ k => $ v ){
Echo "arg". ($ k 1). ": $ v ";
}
}

Foo ();
/* Printsnothing */

Foo ('Hello ');
/* Prints
Arg1: hello
*/

Foo ('hello', 'World', 'Again ');
/* Prints
Arg1: hello
Arg2: world
Arg3: again
*/

...

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.