Php user-defined functions. User-defined functions are also called Udfs. they are not provided by PHP and are created by programmers. because you have created such functions, you can fully control these functions. therefore, user-defined functions are also called Udfs. they are not provided by PHP and are created by programmers. because you have created such functions, you can fully control these functions. therefore, a function can be run in the desired way.
1. declare a function
In PHP, the methods for defining functions are almost the same as those in other programming languages. below is the syntax structure of PHP declarative functions:
Function function_name ($ argument1, $ argument2, $ argument3,... $ argumentn)
{
// Function code
Return value;
}
In the above syntax structure, the meaning of the keyword is as follows.
(1) function: used to declare the keywords of user-defined functions.
(2) function_name: name of the function to be created. this name will be used for future calls. the function name must be unique because PHP does not support overloading. when naming a function, you must follow the same principle as naming variables. however, the function name cannot start with $, and the variable can.
(3) argument: the value to be passed to the function. A function can have multiple parameters with commas. however, parameter items are optional. you can call a function without passing any parameters.
(4) code: A piece of code executed when the function is called. if there are two or more statements, the code must be enclosed in braces. however, if there is only one code, no braces are required.
(5) Return: Return the value required by the called code. any type can be returned, including list and object. this causes the function to immediately end its operation and pass control back to the called row.
2. no parameter functions
Code func_1.php
Require 'a. php ';
Echo "I won't be executed! ";
?>
3. parameter functions
PHP supports passing parameters by value (default), passing parameters by reference and default parameter values. the variable length parameter list is only supported in PHP4 and later versions.
(1) value transfer parameters. passing parameters by value is the default transfer method of PHP. to use this method, you must pass a value (parameter) when calling the main program ).
Code sum. php
Function sum ($ a, $ B)
{
Echo $ a + $ B;
}
Sum (); // start to call this function
?>
(2) reference parameters. when passing by value, only a copy of the parameter is passed to the called function. however, any modification to these values within the called function does not affect the original values in the called function. the reference transfer is actually the address transfer, and the address of a variable is passed as a parameter.
Code valuechange. php
$ MyNum = 100;
Function Valuechange ($ number)
{
$ Number = $ number + 1;
Echo $ number ."
";
}
Valuechange ($ myNum );
Echo $ myNum;
?>
(3) default value parameter. when using the default parameter value transfer method, the function must have a parameter during the call. if no value is used, the default value is passed to the function parameter. the default value must be a constant expression, not a variable, class member, or function call.
Tip: When using default parameters, any default parameters must be placed on the right of non-default parameters; otherwise, the function may not work as expected.
Code func_default.php
$ MyNum = 100;
Function Valuechange ($ number)
{
$ Number = $ number + 1;
Echo $ number ."
";
}
Valuechange ($ myNum );
Echo $ myNum;
?>
...