Home > Article > Backend Development > PHP function definition and function & reference usage
Function definition: 1. A function is a named, 2. Independent code segment, 3. The function performs a specific task, 4. And can return a value to the program that calls it.
Advantages of the function: 1. Improve the program Reusability, 2. Improve program maintainability, 3. Improve development efficiency, 4. Improve software reliability, 5. Control program complexity.
Declaration of function:
function function name () {
}
function function name (parameter 1, parameter 2, parameter...)
{
function body
}
function function function name ()
{
function body;
Return value;
}
function function name (parameter list...)
{
function body;
return value
}
Note:
1. The function must be called to be executed, and can be called before it is declared. It can also be called after declaration.
2. The function name is the same as the variable, aaa bbb ccc aaaBbbCcc (the first word is lowercase, and the first letter of each subsequent word is capitalized)
3. The function cannot have the same name when declaring
4 , you can change the behavior of the function by passing parameters to the function
Formal parameters: When declaring a function, the declared parameters are variables, multiple parameters are separated
Actual parameters: When calling the function, the formal parameter value is passed ( Data (can also be a variable)
5. If there is no return value, it is called a process
6. Return data by using the return statement
7. The function ends when it reaches the return statement. Do not write code after this statement. You can use return to end the execution of the function.
The role of the function name:
1. Call the function and start executing the function
2. You can pass data to the function
3. The function name is the returned value
PHP function: All functions start with the keyword "function()".
Name functions - The name of the function should suggest its function. The function name starts with a letter or an underscore. Add "{" - the part after the opening curly brace is the code of the function.
Insert the function code, add a "}" - the function is terminated by closing the curly brace.
{ echo "David Yang"; } writeMyName(); ?>