The real power of PHP is derived from its functions, but some PHP functions are not fully utilized, and not everyone will read the manuals and function references from page-to-page, which will introduce you to these useful functions and functions.
1, the function of any number of parameters
As you probably already know, PHP allows you to define the functions of optional parameters. But there are also methods that allow any number of function parameters to be fully allowed. The following are examples of optional parameters:
The following is the referenced content:
Functionwith2optionalarguments
Functionfoo ($arg 1= ", $arg 2 =") {
echo "Arg1: $arg 1";
echo "ARG2: $arg 2";
}
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 is the referenced content:
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
*/
http://www.bkjia.com/PHPjc/486209.html www.bkjia.com true http://www.bkjia.com/PHPjc/486209.html techarticle the real power of PHP is derived from its functions, but some PHP functions are not fully utilized, and not everyone will read the manual and the function reference from one page to the next, here ...