Some PHP functions and functions that you don't know before. 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:
- 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
- */
...