Code reuse
Include ()
Require ()
Both functions are used to refer to a file, except that the include () produces a warning when processing fails and require () is a dense error
Include_once ()
Require_once ()
These two functions are the same as include () and require (), except that include_once and require_once can only be referenced once
Custom functions
Custom functions are declared with function ()
The advantages of the function:
Complexity of program Design
Improve the reliability of software
Improve the efficiency of software development
Improve the maintainability of the software
Improve the reusability of programs
Syntax format for custom functions:
Function name (parameter 1, parameter 2) {
Description of the content of the program;
Return
}
Function name (parameter 1, parameter 2);
return value;//return value can also be an expression
Custom function names are not case-sensitive. You cannot use a declared function, or a function name that is built in PHP, when you name a function.
Determine if the function exists: function_exists (function name);
Range of variables
The visibility of variables refers to the scope of the variables in the program.
In general, variables are divided into two types based on declarations: local variables and global variables
Local variables:
A variable declared within a function is a local variable, and the variable can be used only within the scope of the function. If another program needs to invoke the value of the variable locally, it must pass through the "return" instruction to return it to the main program block for subsequent processing.
Global variables:
Variables declared outside the scope of a function are global variables. Because a function can be considered a separate program fragment, the local variable covers the visibility of the global variable, so it is not possible to invoke the global variable directly in the function.
To use a global variable in a function, you must use the Global keyword to define the target variable to tell the function body that the variable is global.
You can also use a predefined array of global variables $globals. This is a special variable that is created automatically when the program is run.
echo $GLOBALS ["A"];
by unset ($var) You can delete a variable manually, and the variable is freed in memory and is not in the global scope.
Using require with include does not affect scope
static variables
Declare the function variable to be static (static).
A static variable is shared among all calls to the function, and is initialized only when the function is invoked for the first time during the execution of the script. To declare a function variable to be static, use the keyword static. Typically, the first use of a static variable is given an initial value.
Passing of parameters
Passing parameters by value:
The parent program passes the specified value or variable directly to the function. Because the values or variables that are passed are stored in different chunks of memory in the function, they do not have a direct effect on the parent program when the function makes any changes to the value being imported.
Pass parameters by address (implemented with "&" symbol)
Instead of passing a specified number or target variable in a parent program to a function, rather than passing it by value, the value or variable's memory block relative address is imported into the function. Therefore, when the value has any changes in the function, it will affect the parent program.
Default parameters
The default parameters must be listed after all without default value parameters.
function fun_sum ($a, $b =0, $c =0) {
Return $a + $b + $c;
}
echo Fun_sum (10,20);
echo Fun_sum (10,20,30);
0 is the default parameter
Any number of argument lists
Func_get_args ()//Returns an array containing all parameters
Func_num_args ()///Total number of parameters returned
Func_get_arg ()///Receive a numeric parameter, return the specified parameter to find the value by subscript
function foo ()
{
$numargs = Func_num_args ();
echo "Number of arguments: $numargs <br/>\n";
if ($numargs >= 2) {
echo "Second argument is:". Func_get_arg (1). "<br/>\n";
}
$arg _list = Func_get_args ();
for ($i = 0; $i < $numargs; $i + +) {
echo "Argument $i is:". $arg _list[$i]. "<br/>\n";
}
}
Foo (1, 2, 3);
Output result: Number of Arguments:3
Second argument Is:2
Argument 0 is:1
Argument 1 Is:2
Argument 2 Is:3
Variable function
This means that if a variable name is followed by parentheses, PHP will look for a function with the same name as the value of the variable, and will attempt to execute it. This can be used to implement callback functions, function tables, and so on, among other things.
Recursive call
The so-called recursive function invocation is that the function can invoke itself in the execution narration of its declaration.
Typically, a conditional judgment narration is appended to the function of this type to determine whether recursive calls need to be performed, and to terminate the recursive invocation of the function under certain conditions, and to return the master control of the current process to the execution of the previous level function. So when a function that executes a recursive call, there is no additional condition to judge the narration, which can cause an infinite loop of error situations.
The biggest advantage of recursive function invocation is that it can streamline repetitive calling programs in programs and perform some more complex operations with this feature.
This courseware is Phpchina teaching courseware
1210491967_9664e02c.rar
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