PHP functions and functions that are unknown

Source: Internet
Author: User
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. 51CTO recommended topic: PHP development basics the real power of PHP comes from its functions, but some PHP functions are not fully utilized, not everyone will read the manual and function reference page by page from start to end. here we will introduce you to these useful 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:

 
 
  1. Reference content is as follows:
  2. // Functionwith2optionalarguments
  3. Functionfoo ($ arg1 = ", $ arg2 = "){
  4. Echo "arg1: $ arg1 \ n ";
  5. Echo "arg2: $ arg2 \ n ";
  6. }
  7. Foo ('hello', 'World ');
  8. /* Prints:
  9. Arg1: hello
  10. Arg2: world
  11. */
  12. Foo ();
  13. /* Prints:
  14. Arg1:
  15. Arg2:
  16. */

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:

  1. Reference content is as follows:
  2. // Yes, theargumentlistcanbeempty
  3. Functionfoo (){
  4. // Returnsanarrayofallpassedarguments
  5. $ Args = func_get_args ();
  6. Foreach ($ argsas $ k =>$ V ){
  7. Echo "arg". ($ k + 1). ": $ v \ n ";
  8. }
  9. }
  10. Foo ();
  11. /* Printsnothing */
  12. Foo ('Hello ');
  13. /* Prints
  14. Arg1: hello
  15. */
  16. Foo ('hello', 'World', 'Again ');
  17. /* Prints
  18. Arg1: hello
  19. Arg2: world
  20. Arg3: again
  21. */

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.