Code reuse and function writing 1. Use the Require () and include () functions
The purpose of these two functions is to load a file into the PHP script so that you can invoke the method in the file directly.
Require () and include () are almost the same, the only difference is that the function fails with a fatal error, and the latter gives a warning
Variants: require_once () and include_once () ensure that a contained file can only be introduced once, using this
2. Calling functions in PHP using function 2.1
If a function is already defined, and the function is in this script, it can be called directly, similar to the call function $fp = fopen($name, $openmode); , this is the return of the call function to assign a value to a variable, of course, can not be assigned to the variable, directly use. But if this function is not in the script and you want to use it, you can use the Require () function to include the script in which the function is located, and it can be called directly as above.
2.2 Calling undefined functions
Calling the function defined will give an error, this time to check two things:
1. Check that the spelling of the function name is correct, the PHP identifier is case-sensitive, but the function name is not distinguished.
2. Check if this function exists with the PHP version you are using
3. Reference passing of parameters and value passing 3.1 value passing
PHP receives parameters in two ways, one is the value of the transfer, that is, the value of the variable is passed directly into the function, but this transfer is to copy the value of the variable to a function, so there is a problem when the function of the internal operation does not change the value of this variable, sometimes referred to as the problem of local variables, If you want to solve this problem, you need to declare this as a global variable in the function.
See an example:
1 functionIncrement$value,$amout= 1){2 $value=$value+$amount;3 }4 5 $value= 10;6Increment$value);7 Echo $value;//Ten
3.2 Reference Delivery
Reference passing can solve the above problem, the idea is: Do not pass the copy of this parameter when passing, but pass the variable reference to the value, explain that when a value is assigned to a variable, this variable holds the address of this value. So we can add a & in front of the variable to determine that we want to receive the value of the variable to save the corresponding address, so that when we re-operation is the direct operation of the variable corresponding to the value.
Look at an example:
function Increment (&$value$amout = 1) { $value$value $amount ;} $value = ten; increment ($value); Echo $value // One
4. Use the return keyword
The keyword return terminates the execution of the function, and in the function, if it encounters a return, then the function executes the return and no longer executes the command following the return.
PHP code reuse and function writing