In PHP, programmers are allowed to organize commonly used components such as processes or variables into a fixed format. That is to say, you can combine functions or classes on your own. In PHP, programmers are allowed to organize commonly used components such as processes or variables into a fixed format. That is to say, you can combine functions or classes on your own.
Functions in PHP are the same as those in C, including returned values? And no return value ?, Unlike Pascal's function and procedure.
In terms of function names, PHP is very loose in case sensitivity. You can write an upper-case name when defining a function, and use a lower-case name when using the function. In short, for a function, it is case-insensitive, as long as the name is not repeated.
The following is the usage syntax of the function.
Function myfunc ($ arg_1, $ arg_2,..., $ arg_n) {// execute some steps return $ retval ;}
When using the function, you must add a reserved word of the function before the custom function name, indicating that this is a custom function. Myfunc can be a string starting with any English letter. a string cannot start with a number or underscore, but can be an Arabic number or underscore after the first letter, of course, other symbols or text cannot be used as function names.
$ Arg_1 to $ arg_n are the parameters used by the function. they are separated by commas. Braces {} after the parameter, that is, the part of the entire function. If the function returns a value, you can use return to return the value. Parameters can be pre-defined with initial values or default values. Define the default value? The parameter can be omitted when using the function, but it must be placed after the default value parameter is not configured. otherwise, PHP will encounter an error when parsing the function.
In addition, the parameter type can be used as long as the parameter is a variable type supported by PHP, whether it is an array, a string, or an integer... and so on. The same is true for returned values.
Which of the following are the default values? Example
Function myfunc1 ($ arg_1, $ arg_2, $ arg_3 = "I am the default string "){
Echo $ arg_1 + $ arg_2;
Echo $ arg_3 ."
\ N ";
}
Myfunc (3, 4); // The parameter $ arg_3 is omitted.
Myfunc (6, 6, "Do not use the default value") // enter the parameter $ arg_3.
?>
What is the parameter value? You can use the address transfer method to pass in the parameter pointer when it is worth the request. The method is to add the & symbol before the parameter. Example
Function myfunc2 (& $ argstr ){
$ Argstr = ereg_replace ("/", "-", $ argstr );
}
$ Today = "2000/01/01 ";
Myfunc2 ($ today );
Echo $ today; // 2000-01-01;
?>