Some PHP functions and functions that you don't know before

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. 1. functions with any number of parameters you may already know. PHP allows defining functions with optional parameters. But there are also fully permitted "> <LINKhref =" http://www.php100.co

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:

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

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.  
  3. // Yes, theargumentlistcanbeempty
  4. Functionfoo (){
  5.  
  6. // Returnsanarrayofallpassedarguments
  7. $ Args = func_get_args ();
  8.  
  9. Foreach ($ argsas $ k =>$ V ){
  10. Echo "arg". ($ k + 1). ": $ v \ n ";
  11. }
  12.  
  13. }
  14.  
  15. Foo ();
  16. /* Printsnothing */
  17.  
  18. Foo ('Hello ');
  19. /* Prints
  20. Arg1: hello
  21. */
  22.  
  23. Foo ('hello', 'World', 'Again ');
  24. /* Prints
  25. Arg1: hello
  26. Arg2: world
  27. Arg3: again
  28. */

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.