Php (4) -- function

Source: Internet
Author: User
: This article mainly introduces php (4)-functions. For more information about PHP tutorials, see. Functions in PHP do not need to be defined before being called;

All functions and classes in PHP have a global scope, which can be defined within a function and called outside, and vice versa;

PHP does not support function overloading, and it is impossible to cancel or redefine declared functions;

You can call recursive functions in PHP. However, to avoid calling recursive functions/methods that exceed 100-200 layers, the current script may be terminated because the stack may crash.

Function definition:

Function name ([parameter list]) {

Function body

}

PHP uses the value transfer method by default, but can also pass references (this method can change the passed parameter value in the function body)

For example:

Function fun (& $ var ){

$ Var ++;

}

$ Var = 0;

Fun ($ var );

Echo $ var;

Output 1;

PHP supports default parameter values.

For example:

Function fun ($ var1, $ var2 = 2, $ var3 = 3 ){
Return $ var1 + $ var2 + $ var3;
}

Echo fun (1 );
Echo fun (1, 1 );
Echo fun (1, 1, 2 );

Output 6 5 4

Note: Any default parameter must be placed on the right of any non-default parameter; otherwise, the function will not work as expected.

For example, change the preceding function:

Function fun ($ var2 = 2, $ var3 = 3, $ var1 ){
Return $ var1 + $ var2 + $ var3;
}
Echo fun (1 );
Echo fun (1, 1 );
Echo fun (1, 1, 2 );

In addition to the normal execution of the third call method, problems may occur in the first two methods.

PHP supports a variable number of parameter lists.

Before PHP5.6, you need to use func_num_args () to obtain the parameter information and func_get_arg (I) to obtain the value of the I parameter;

For example:

Function fun (){
$ Len = func_num_args ();
$ Res = 0;
For ($ I = 0; $ I <$ len; $ I ++ ){
$ Res + = func_get_arg ($ I );
}
Return $ res;
}

The... $ args method is introduced in PHP5.6,

For example:

Function fun (... $ args ){
$ Res = 0;
Foreach ($ args as $ val ){
$ Res + = $ val;
}
Return $ res;

}

The results of the two methods are the same.

The concept of variable functions in PHP

That is, if a variable name has parentheses, PHP searches for a function with the same name as the variable value and tries to execute it. Variable functions can be used for some purposes, including callback functions and function tables.

Example:

Function fun (){
Echo "Hello ";
}
$ Var = "fun ";
$ Var (); // The fun () function will be called.

Anonymous functions in PHP

An Anonymous function (Anonymous functions) is also called a closure function (Closures), Allows you to temporarily create a function without a specified name. The value that is most often used as the callback function parameter.

You can also assign values to an anonymous function, such:

$ Fun = function (){
Echo "HelloWorld ";
};
$ Fun ();

The above introduces php (4)-functions, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.