Functions ____ Function in PHP

Source: Internet
Author: User

Functions in PHP

Functions, because PHP is a weakly typed language, this not only makes PHP variables unique, but also makes PHP functions unique.
The way functions are declared in PHP is simple:
function fcuntion_name ([parameter list])
{
}
Functions can be grouped into several categories:
(1). general parameter function, is the parameter is standard. The value of the variable parameter in the function has no effect on the original variable.

(2). A function that refers to a parameter, that is, the passed parameter is a reference to a parameter, changing the value of the parameter in the function affects the value of the original variable. But using the unset () function to unregister a reference variable in a function
Simply logging off the corresponding variable in the function does not unregister the original variable. As follows:
$t = 2;
Function test (& $m)
{
$m =5
Unset ($m)
}
Test ($t);
Echo $t; Output 5, that is, changing the value of a reference parameter changes the value of the original variable, but unregistering the reference parameter in the function, simply unregistering the reference parameter to point to the original variable address instead of Unregistering the original variable.

(3). The default parameter function, that is, when the function is defined, some parameters are assigned, and the default parameters are arranged from right to left. Such as
function User ($name, $pwd =123, $stu _no= ' 20132577 ')
{
}
When invoked, the default value of the parameter is used if there is no value assigned to the parameter that already has a default value. If you assign a value to the default parameter at the time of the call, the original default value is overwritten.

(4). A function that has a variable number of parameters, that is, the parameters of a function are indeterminate when defined, and the number of parameters can only be determined when the function is invoked.
Such functions require an array of all the arguments entered when the function Func_get_args () is obtained using the PHP system's own function.
or the number of input parameters when the call function is obtained by Func_num_args (). And with the Func_get_arg ($index) to obtain the input parameters at the specified position.
The definition is as follows:
function test ()
{
$args =func_get_args (); Gets the array that contains all the input parameters at once,
for ($i =0; $i < $count ($args); $i + +)
{
......

}
}
Or you can get the appropriate parameters by specifying a location.
function test ()
{
$length =func_num_args (); To get the number of input parameters,
for ($i =0; $i < $length); $i + +)
{
$arg =func_get_arg ($i);//The parameter value is obtained from the parameter index.
}
}

(5). variable function, variable function in fact and variable variable is the same principle.
Such as: Define a function test ()
function test ()
{
}
$t = ' test ';//We can call him a variable function, the value of which is a string of function names. $t ();//Call Function test ()

(6). callback function, the callback function is to use the variable function as a parameter to achieve function extension.
such as defining a user function and a identiyt function such as:
function Identiyt ()
{
}

function User ($function _variable)
{
$function _variable ();//user function callback function called $function_variable.
}

$function _variable= ' Identiyt ';
User ($function _variable);//callback identity function.

Generally we can customize the callback function Call_user_func_array ([variable function],[parameter array]) by system function
function T ()
{
echo "t function <be/>";
}
function y ($fun)
{
Call_user_func_array ($fun, Array ());
echo "y function <br/>";
}
Y (' t ');
Using the Call_user_func_array () function, you just call the global function, and you can't call the function in the class, the static function in the class. How are the functions and static functions defined in that class invoked?
Functions in class: Above using Call_user_func_array (Array (new Class (), function_name), array ([parameters])
Static functions in a class: Functions in a class: Above using Call_user_func_array (Array (class,function_name), array ([parameters])

(7) Anonymous functions, also known as closure functions, and JS in the same anonymous function.
$t =function () {};//assigns an anonymous function to a variable $t
The anonymous function is called $t ();

(8) Closure

        Closures can inherit variables from the parent scope. Any such variable should be passed in using the use language structure. How to understand it.   Because we know that a variable defined in a function is a local variable, we define a closure function (anonymous function) in a function that cannot be inherited directly from his parent function. Because the closure function has the inability to get global variables, as the parent function, through global, the variables defined in the parent function are obtained from the parent function, so the closure function requires a new way of inheriting, which is called a closure. Keyword is use
      For example:
 function add1 ()
{
        $a =10
        $b = 20;
        $callback =function () use ($a, $b) to inherit two variables from the parent function by using the, and to change the variable of the parent function if not only to get the value of the variable,
 {                              //via reference to inherited variables such as use (& amp; $a,& $b)
       echo $a + $b;
       };
        $callback ();
 }
            ADD1 ();
A closure has the benefit of reducing the number of loops by inheriting references to variables in the parent function.
 
 

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.