In php, functions are commonly used as a reasonable method that can be used repeatedly. functions are system functions and user-defined functions, respectively, the following describes how to use these two php functions.
In php, functions are commonly used as a reasonable method that can be used repeatedly. functions are system functions and user-defined functions, respectively, the following describes how to use these two php functions. For more information, see.
System functions
For example, phpinfo (); this is a system function. For more information about system functions, see The php Tutorial or manual.
User-defined functions
Php introduces the function concept to make the program concise and faster. A function is a program that implements a certain function. this program is equivalent to a module. you can use some simple commands to call it. you don't have to rewrite it every time. just like a computer, we don't need to know the complex structure in it, as long as we know the functions provided by it can be used directly.
Php user-defined functions-although php has many built-in practical functions, it cannot meet the actual design requirements. at this time, we need to create functions by ourselves.
Basic structure:
The instance code is as follows:
- Function name (parameter ){
- Statement body
- }
Function is a declared function. the function name must be unique. different from the variable name, the function name is case-insensitive. there must be spaces between the function name and the function name. The parameter can be omitted, you can also add multiple parameters separated by commas.
Php calls a function-when we define a function, we can call the function in the following structure:
Function name (parameter)
The parameter can be omitted, but parentheses must be kept.
The instance code is as follows:
-
- Function e (){
- $ I = 5 + 6;
- Echo $ I;
- }
- E ();
- ?>
Function transfer parameters
If a function defines a parameter, you must pay attention to the passing of the parameter value when calling the function. there are two ways to pass php function parameters: pass by value and pass by reference. the default value is pass by value.
1. pass by value:
The instance code is as follows:
-
- Function sum ($ num, $ price ){
- $ Total = $ num * $ price;
- Echo "total price: $ total ";
- }
- Sum (10, 78 );
- ?>
Default parameters
Default php function parameter-if the value of this parameter is not provided when a function is used, this parameter uses the default value set by the function. the default value must have been assigned when the function is defined.
The instance code is as follows:
-
- Function add ($ a, $ B = 10) {/* defines the variable $ B default parameter */
- $ Sum = $ a + $ B;
- Echo $ sum ."
";
- }
- Add ();/* pass parameters by value instead of default parameters */
- Add (1);/* Default value */
- ?>
Function return value
When designing a program, we often do not want to display the function running result in the browser. in this case, we can call the function value through the return statement before performing other operations.
Php function return value instance:
The instance code is as follows:
-
- Function add ($ a, $ B = 10 ){
- $ Sum = $ a + $ B;
- Return $ sum;/* assign the value of $ sum to the function */
- }
- $ Sum = add ();/* the variable $ sum here gets the function value */
- Echo $ sum ."
";
- ?>
Nested function call
The function can not only be a general execution statement, but also a function. this is the use of nested php functions. function nesting only calls other functions in a function. here we need to understand that all functions are in a parallel relationship and function definitions are independent. just like a container can hold many small containers, which are independent of each other and can hold their own things.
The instance code is as follows:
-
- Function volume ($ l, $ w ){
- Function height ($ ){
- $ A = $ a/2;
- Return $;
- }
- $ A = $ l * $ w;
- $ V = $ a * height ($ );
- Echo $ v;
- }
- Volume (8, 3 );
- ?>
Recursive function call
What is a php recursive function? Before explaining this concept, let's look at a mathematical example. if we want to calculate the factorial of n, that is, n !, First, you should know (n-1 )!, But you need to know (n-1 )!, And must know (n-2 )!. And so on until 1! = 1, then 2 is returned !, 3 !... (N-1 )!, N !, In order to finally obtain the n factorial result.
The recursive call of a php function means that a function can call itself. This type of direct or brief call of the function itself is allowed in the php language and is a recursive function.
The instance code is as follows:
-
- Function calculate ($ n ){
- If ($ n = 0)
- Return 1;
- Else
- Return $ n * calculate ($ n-1 );
- }
- $ S = calculate (3 );
- Echo $ s;
- ?>