Home > Backend Development > PHP Tutorial > Some PHP functions and features you never knew_PHP Tutorial

Some PHP functions and features you never knew_PHP Tutorial

WBOY
Release: 2016-07-13 17:39:22
Original
765 people have browsed it

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 from beginning to end. Here we will introduce it to you These useful functions and functions.

1. Function with any number of parameters

As you may already know, PHP allows defining functions with optional parameters. But there are also methods that completely allow any number of function parameters. The following are examples of optional parameters:

  1. The following is the quoted content:
  2. //functionwith2optionalarguments
  3. functionfoo($arg1=”,$arg2=”){
  4. echo "arg1:$arg1 ”;
  5. echo "arg2:$arg2 ”;
  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 accepts any number of arguments. This time you need to use the func_get_args() function:

  1. The following is the quoted content:
  2. //yes,theargumentlistcanbeempty
  3. functionfoo(){
  4. //returnsanarrayofallpassedarguments
  5. $args=func_get_args();
  6. foreach($argsas$k=>$v){
  7. echo “arg”.($k+1).”:$v ”;
  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. */

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486310.htmlTechArticleThe real power of PHP comes from its functions, but some PHP functions are not fully utilized and are not fully utilized. Not everyone will read the manual and function reference from cover to cover, here...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template