php basic tutorial step 9 custom function

Custom function

In the previous course, we have used php system functions, these functions are defined by the system. During development, sometimes the built-in functions of the system cannot satisfy our development (or other situations), we can customize the function. As the name implies, a custom function is a function defined by yourself.
The syntax of the custom function is as follows:

<?php
function 函数名称()
{
    
    
    该函数要执行的一些代码
}
?>

In the above custom function, the keyword is function followed by the function name, which is defined by yourself. It is followed by a parenthesis, and then a curly brace. Filled in the curly braces is some code that needs to be executed by the current function.

The most basic custom function

After reviewing the basic syntax of the above custom function, check the following examples to understand the custom function intuitively:

<?php
function printInfo(){
    
    
	echo 'hello';
}
printInfo();
?>

According to the custom function definition grammar, in the above code, a function is defined using the function keyword. This function is named printInfo and the code to be executed is echo 'hello';.

After defining the function, the following function call using a method 函数名();called printInfo function printInfo();.

Open the service, access the php file, the result is as follows:
Insert picture description here
the code is successfully executed echo 'hello';.

Function with parameters

parameter? What are parameters? There may be some beginners who will raise this question. Before understanding the concept of parameters, we need to know what a function is. Function can be regarded as a function, like cooking. The function of a rice cooker is to cook, but what raw materials are needed to cook? Of course, you need rice to cook, and of course, you also need water. This rice and water are the parameters.

Back to the topic, when we write a function, to complete the addition of two variables, then I need to give two variables to this function. Then these two variables are parameters. Check out the following example to learn about a custom function for adding two numbers:

<?php
function sum_($a,$b){
    
    
	echo 'a+b='.($a+$b);
}
sum_(1,2);
?>

In the above code, a function called sum_ is defined. You can give this function two parameters. The first parameter is a and the second parameter is b. The two parameters are separated by a comma (multiple parameters can be , More than 2 parameters can be passed). In the code of the function, output the value of a+b.
Novice readers can view 'a+b='.($a+$b)the code can be found 'a+b='with ($a+$b)the use of a decimal point between. Link, why not write 'a+b='.$a+$bit? If 'a+b='.$a+$bso, the computer not directly calculate two variables a + b, and directly use the decimal link variables a, so that equals a + b = '. $ A is then coupled with the variable b. But because the previous value is a string, the latter value is a number. In general, different types cannot do operations such as addition and subtraction, so we need to use parentheses to calculate the variable a+b to get a Number, then connect.
Then call the sum_ function, and in the parentheses of the function, the first value is written as 1, this 1 corresponds to the variable a in the function, the second is written 2, and this 2 corresponds to the variable b in the function . Here we find that the value of the passed parameter can be corresponded according to the position.
The final result is as follows:
Insert picture description here

Return value function

After learning the above parameterized functions, the return value function is also very fast to learn. The return value function is a function that returns a value. For example, we just calculated a+b, we need to return the result to a variable to receive, and assign the result of a+b to a variable. Check the following example to complete:

<?php
function sum_($a,$b){
    
    
	return $a+$b;
}
$a=sum_(1,2);
echo $a;
?>

In the above code, the return keyword is used in the sum_ function, and the function of the return keyword is to return a value. Here writing return $a+$b;represents the return value of a + b.
When calling the function, we write as $a=sum_(1,2);, here, we assign the a+b value of return in the sum function to the variable a. Then output the value of variable a: the
Insert picture description here
output is 3 and the result is correct.
In the above example, we find that after calling the function, the returned value is given to the variable a, but the function accepts a parameter that is also a variable. Are these two variables the same value? The answer is " These two variables are not the same value ."
How to understand these two variables? For example, there are two people in your school called Xiao Ming, and your principal is also called Xiao Ming. There is also a Xiao Ming in your class. When you say Xiao Ming in the class, it is of course that Xiao Ming is in your class. The Xiao Ming who speaks outside the class must be the principal. . So this variable a in the function means the variable a in the function. Outside the function means the a variable outside the function.

In order to facilitate understanding, the code in my above function example is all one line, but the function in the function is not only one line. You can try it yourself to see the effect. Another point to remind here is that the use of return in the function means that the function jumps out here and ends the execution of the function. You need to pay attention to the use of the function. When the function logic is not completed, or the logic you want is not completed, Don't use return, it will make your function jump out and end the function.

Guess you like

Origin blog.csdn.net/A757291228/article/details/107346966