The concept and role of function
Function is not a number! A function is a form of code (grammatical form).
A function is a whole package of "several lines of code" in a grammatical form. The whole can be "executed when needed" (that is, execute several lines of code).
Function is an effective way to solve the need to execute the same code under different situations (different code locations) - the so-called code reuse.
Definition and calling of functions
//定义语法形式: function 函数名($形参1,$形参2,..){ 函数体语句块; //就是若干行代码 } //调用语法形式: 函数名($实参1,$实参2, ...... );
Instructions:
Formal parameters are actually variables, which can only be used inside the function;
actual parameters are actually data, which will be passed in Data inside the function (assigned to formal parameter variables in one-to-one correspondence);
Function return value
What is the return value of a function?
含义: 一个函数在执行结束时,可以让其返回一个数据,这就是函数的返回值。 语法: return 要返回的数据;
Explanation:
The return value obtained by a function execution can be used in any situation where data is required, just like using a The variable data is the same.
In addition, the return statement does not need to contain the following data. In this case, it simply ends the function and does not return the data. (It can also be said to return null, the empty data).
##Little test:
Now let us define a function ourselves, Calculate the surface area of a cuboid of any length, width and height.<?php //定义一个函数,可以计算任意长宽高的一个长方体的表面积 function biaomianji($chang,$kuan,$gao) { $mianji = ($chang*$kuan + $chang*$gao + $kuan * $gao)*2; return $mianji; } $biaomianji2 = biaomianji(1,2,3); echo ("表面积为:$biaomianji2");
##Summary:
In this way, when we develop the program, if a certain piece of code needs to be executed many times, we can encapsulate this piece of code into a function, thereby improving development efficiency and code reuse.The above is the detailed content of The concept and role of PHP functions. For more information, please follow other related articles on the PHP Chinese website!