Php defines the basic form functions of function (1:
A function is a named independent code segment. it executes a specific task and returns a value to the program that calls it.
Advantages of functions:
1. improving program reusability 2. improving program maintainability 3. improving software development efficiency 4. Improving Software reliability 5. controlling program complexity
Notes for using functions:
1. a function must be called before execution. it can be called before declaration or after function declaration.
2. function names must be meaningful just like variables
3. the function name cannot be repeated during declaration.
1. function name (){
Function body
}
2. function name (parameter 1, parameter 2, parameter...) // list of parameters. if multiple parameters exist, separate them.
{
Function body
}
3. function name (){
Function body
Return value;
}
4. function body (parameter list ){
Function body
Return value
}
Type 1: no parameter function,Example:
Function a () {// function 1 without parameters, output a statement
Echo "this is the first function ";
}
Function table () {// no parameter function 2, which outputs a 10x10 table
Echo'
';Echo'
Table name
';For ($ I = 0; $ I <10; $ I ++ ){If ($ I % 2 = 0)$ Bg = "# ffffff ";Else$ Bg = "# cccccc ";Echo'
';For ($ j = 0; $ j <10; $ j ++ ){Echo'
'. ($ I * 10 + $ j ).' | ';}Echo'
';} // This part is a for loop statement that controls the form of table output.Echo'
';
}
A (); // call 1
Echo"
";
Table (); // call 2
?> // No-argument function is a process to implement a function
Type 2: argument function. example:
Function activity ($ name, $ time, $ event) {// function 1 with parameters, output a statement
Echo "$ name". 'In '. "$ time". 'punctual'. "$ event ";
}
Function sum ($ x, $ y) {// output a value. after return is used to obtain the returned value, add echo
$ Sum = $ x * $ x + $ y * $ y;
Return $ sum;
}
Activity ("James", "", "dinner ");
Echo"
";
Echo sum (4, 5 );