PHP provides powerful functions, but this is far from meeting the needs. Programmers can create functions by themselves as needed. In this section, we begin to learn how to create functions.
In the actual development process, we need to have many functions that need to be used repeatedly. If we can define these functions that need to be used repeatedly, we can define them as functions (functions) as much as possible ( function). When using it, just shout its name.
Then let’s learn the grammatical provisions of custom functions. The grammatical provisions are as follows:
function 函数名([参数名1[=值1], 参数名2[=值2], 参数名n[=值n]]) { 函数中的功能体 [return 返回值] }
The following characteristics are found in the above grammatical provisions, resulting in the following unspecified expression:
1. The function starts with function
2. Function is followed by a space, and the space is followed by the function name
3. The naming rules for function names and variables are basically the same, but the difference is: function name It is not case sensitive
4. The so-called parameters are actually variables
5. The function name is followed by brackets, and the parameters are enclosed in brackets. All parameters are enclosed by [] (square brackets), which means The parameters can be filled in or not
6. If there are parameters, the parameters can be followed by an equal sign (=), and the equal sign is followed by the default value. Parameter values are also enclosed in [] (square brackets), indicating optional
7. The main function of the parameter variables after the function is to pass the variable values outside the function into the function body for use. The function body The variables and the variables outside the function are usually two different variables.
8. The specific function (function body) in the function is enclosed in curly brackets, indicating that this is the function range of a function
9. A function can have a return value or no return value. Those enclosed by [] (square brackets) represent optional fields.
10. Return is followed by a space, and a space is followed by the return value. If there is a return, the code after the return will not be executed.
11. There is no order relationship in the execution of functions. They can be called before the definition.
12. A function cannot be defined twice, that is, the function cannot be overloaded
Remember: You also need to write more code to conduct experiments!
We can prove these points bit by bit through experiments.
1. The function is like a dog. Wherever it is called, it will follow you.
<?php function php_cn(){ echo '我是一条狗'; } php_cn(); php_cn(); php_cn(); ?>
php_cnDoes this function display three paragraphs: I am A dog?
2 Function name can only be a combination of letters, numbers, and underscores, and cannot contain spaces. Numbers cannot be placed at the beginning of the variable name.
<?php function 1demo(){ } ?>
The above code will report an error. Function naming and variable naming
3. Function names and variable naming rules are the same, but the difference is: function names are not case-sensitive
<?php function Demo(){ echo '如果是写代码的男人,我们需要更加承担来自家庭的责任<br />'; } demo(); Demo(); DEMO(); ?>
Through the above code, you will find that three lines will be displayed: If it is a man who writes the code, we need to take more responsibility from the family
.
That is, the function name is not case-sensitive when called. A function can have its name called repeatedly and be executed repeatedly. It also reflects the characteristics of reuse.
4. If the parameters of the function body are defined and no parameters are passed, the code will report an error
<?php //定义函数名为test,必须要传入一个参数 function test($hello){ } test(); ?>
Write a paragraph yourself and try it. Will it report an error?
5. If the parameters after the function have default values, the parameters do not need to be filled in, and the code will not report an error.
<?php function test( $arg = 10){ echo $arg; } test(); test(88); ?>
Is there no error reported? And it showed 10 and came out.
Note that if parameters are passed, they will be brought into the function. If the function does not pass parameters, the default value after the parameters will be used.
6. You can write multiple parameters after the function
<?php function test( $a , $b = 20 , $c = 30){ echo $a + $b + $c; } test( 1 , 2 , 3 ); ?>
Write the above code yourself. Have we passed in multiple parameters?
7. If there are parameters with default values and no default values after the function, the parameters without default values are usually written in the first 3rd and 3rd numbers before
From 4 experiments, we found that the default value means that this parameter does not need to be passed in. If there is no default value, the code will report an error, that is, parameters without default values must be passed. Let's take a look at the following example:
<?php function test( $a = 20 , $b = 30 , $c ){ echo $a + $b + $c; } //重点:重点看这一行执行一下 test( , , 8 ); ?>
Through the above example, we found that another error was reported when executing the above code. That is to say, the above syntax will not work.
What we hope in the above syntax is that we do not pass in any values for parameter $a and parameter $b. The parameter $c must be passed in, we passed 8. But PHP's grammatical regulations do not allow us to write this. Therefore, we can achieve the same effect by changing the way of writing:
<?php function test( $c , $a = 20 , $b = 30){ echo $a + $b + $c; } //重点:重点看这一行执行一下 test( 8 ); ?>
Through the above example experiment, did you find that I passed the parameter $c and the code did not report an error. The parameter $a = 20 and the parameter $b = 30 are also brought into the code $a + $b + $c.
8. There is no relationship between the variables in the function body and the variables outside the function
<?php //定义变量$hello的值为10 $hello = 10; //函数后的参数(形式上的参数,参数)处写上变量名为$hello function demo( $hello ){ //形参带入了函数体内,函数体内变量$hello 改为了 250 $hello = 250; //输入2个250相加的结果 echo $hello + $hello; } //将函数体外的变量$hello,传入变量的参数处(实际传入的参数,实参),显示出的结果为500 demo($hello); //此处$hello的值输出,依然为10 echo $hello; ?>
Through the above example, we found that there is no change point relationship between the formal parameters and the actual parameters. No matter how the formal parameters passed in the function body change, it will not affect the actual result of the actual parameter $hello.
9. If there is a return in the function body, the code after the return will not be executed.
<?php function demo(){ echo 111; return; echo 222; } demo(); ?>
Have you noticed? ——Only 111 is output.
10. After the function is executed, return can bring the value in the function body out of the function body
<?php //定义一条函数狗 function php_cn(){ $foo = 5; $bar = 6; $result = $foo + $bar; //将$result的结果进行返回 return $result; } //调用php_cn()这个函数,$foo和$bar相加的$result就会返回回来给到变量$piao $piao = php_cn(); //输出$piao的结果,果真为11 echo $piao; ?>
11. There is no order relationship in the execution of functions. It can be called before the definition
<?php demo(); function demo(){ $str = '爸爸妈妈年龄越来越大'; $str .= '大多数的孩子都是独生子女,更加应该负起责任'; echo $str; } demo(); ?>
Through the above example, we found that the function can be called before or after the definition. That is, the function can be called anywhere.
11. Functions cannot be overloaded
<?php function demo(){ } function demo(){ } //试试会报错吗? ?>
It is found through the above example that an error is reported, that is, the function with the same name cannot be defined twice, otherwise an error is reported
Assignment:
1. Define a double-layered loop with alternating rows of color-changing tables
2. Require this form, the default values are 10 and 10, one of the colors is a default parameter, and one has no default parameters
3. Return the string of the table back
Next Section