PHP (4)--function

Source: Internet
Author: User
the functions in PHP need not be defined before they are called;

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

PHP does not support function overloading, and it is not possible to cancel the definition or redefine a declared function;

Recursive functions can be called in PHP. However, to avoid recursive function/method calls over 100-200 layers, because the stack might crash and then terminate the current script.

Definition of the function:

function name ([parameter list]) {

function body

PHP defaults to the way value is passed, but you can also pass a reference (this way you can change the value of the parameter passed in the function body)

Such as:

Function Fun (& $var) {

$var + +;

}

$var = 0;

Fun ($var);

Echo $var;

Output 1;

PHP supports default parameter values.

Such as:

function Fun ($var 1, $var 2=2, $var 3=3) {
return $var 1+ $var $var 3;
}

echo Fun (1);
echo Fun (+);
echo Fun (1,1,2);

Will output 6 5 4 respectively

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

If you change the above function to:

function Fun ($var 2=2, $var 3=3, $var 1) {
return $var 1+ $var $var 3;
}
echo Fun (1);
echo Fun (+);
echo Fun (1,1,2);

In addition to the third method of invocation can be normal execution, the first two of the problems will occur.

PHP supports a variable number of parameter lists.

Before PHP5.6, obtaining the information of the parameter needs to use Func_num_args () to obtain the number of parameters, Func_get_arg (i) to obtain the value of the parameters of the first parameter;

Such as:

function Fun () {
$len = Func_num_args ();
$res = 0;
for ($i = 0; $i < $len; $i + +) {
$res + = Func_get_arg ($i);
}
return $res;
}

The introduction of the PHP5.6 in the ... $args way,

Such as:

Function Fun (... $args) {
$res = 0;
foreach ($args as $val) {
$res + = $val;
}
return $res;

}

The results are the same in both ways.

The concept of variable functions in PHP

That is, if a variable name has parentheses behind it, PHP will look for a function with the same name as the value of the variable and try to execute it. Variable functions can be used to implement some purposes, including callback functions, function tables, and so on.

Cases:

function Fun () {
echo "Hello";
}
$var = "Fun";
$var ();//The Fun () function will be called

anonymous functions in PHP

The anonymous function (Anonymous functions), also called the closure function (closures), allows temporary creation of a function without a specified name. The value most often used as the callback function (callback) parameter.

You can also assign an anonymous function to its function name by assigning a value, such as:

$fun = function () {
echo "HelloWorld";
};
$fun ();

The above introduces PHP (4)--functions, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.