Function
Similar to JS function, focus on PHP and other languages do not use the place
The default value of the function's shape
Form:
function f($a,$b=3,$vc = "abc"){}
Note: You must pass a value without a parameter to the default value.
The problem of the transfer value of the form participation
In a word: the problem of the value of the form. In fact, it is "the value before the variable", that is, the actual variables passed to the shape of the variable. There are value passing and reference passing
Reference Pass (requires the & symbol instead of the actual participation before the shape):
function f($a,& $b){}
The problem of the number of function parameters
- In general, the number of formal and actual participation is consistent;
- When the function has a default value. The actual participation can be omitted in a certain degree (omitted only from right to left).
There is a special form of defining and using functions (not common) it does not define a formal participation, and the actual participation is given. In fact, there are similar functions in the system: for example: – One of the flexibility embodied
- Var_dump ($v 1);
- Var_dump ($v1,$v2,$v3);
We can also define such a function ourselves. In fact, such a function. The following three system functions are relied upon to obtain the appropriate information. To get the actual data processed:
- Func_get_args (); Get the list of real-world data and become an array
- Func_get_args ( I);//receivedTakeSection I data, $i start from 0
- Func_num_args (); Get the number of actual participation (number)
Such as:
// 系统函数func_get_args()能够获取函数调用时传递过来的全部实參数据,而且都放入一个数组中!function f(){ $arr = func_get_args();}
function return value: The return value of the function is not a syntax rule. It is the business that needs to be returned. No need to return, the return value must be through the return statement!
Other forms of the function:
- Variable function : The name of a variable is a variable –> flexibility embodies
- mutable variable : The name of a function is a variable, in fact, when the function is called, a variable name is used, the inside of the variable is the function name! –> Flexible representation
anonymous functions
Form 1: Assign an anonymous function "assignment" to a variable, and then call the function through that variable
$f1 = function (){};
Use:
$f1(); // 调用该匿名函数,和可变函数使用一样的
Form 2: The anonymous function is directly passed in as an argument to another function (because a function is corresponding to the formal participation)
Such as:
function f1($a,$b,$sum){ echo "$a =".$a; echo "$b =".$b; $result = $sum($a,$b); echo "result = ".$result;}调用:f1(21,2,function ($a,$b){ returm $a + $b;});
Variable scope
Definition: The range of variables that can be used
There are three types of scopes in PHP
- Local scope: Within the scope of the function, the corresponding variable is a local variable
Global scope: Not within the scope of the function, – the variables defined in the outside of the function and in the code block are global variables
Hyper Global scope: refers to the entire range of code. is called a hyper-global variable. Infact there are only a few of the systems defined in advance:$_get,$POST,$_server,$_session, $ _request,$ GLOBALS,$_files,$_cookie..
Usually:
- Global scope cannot access local scope;
- Local scope cannot access global scope;
- Variables inside the function (local variables) are usually "destroyed" after the function call has finished running;
- There is a local variable that is not "destroyed" after the function call ends, and the variable is a static local variable (modified with static).
Local scope use (access) global variables? (Common needs)
php中不能像其它语言一样直接在局部作用域中訪问全局变量,做法1:而要使用global声明一个要使用的全局变量的同名局部变量 如: $v = 4; function f(){ global $v; // 使用global声明一个变量,该变量和全局变量同名。并和全局变量共同指向一个数据区,,相当于复制了引用 }做法2:通过$GLOBALS提前定义变量訪问全局变量,如 $v = 1; function f(){ echo "\$v = ".$GLOBALS[‘$v‘]; // 取得全局变量$v的值 $GLOBALS[‘$v‘] = 55; // 改变全局变量$v的值 unset($GLOBALS[‘$v‘]); // 释放$v变量,此时全局变量$v就变成null了 }但,假设我们对$GLOBALS变量的某个单元(下标)进行unset,则其就会全然销毁相应的全局变量。这里不是复制了引用
system functions related to functions:
- Function_exists (function name): Infer whether a function exists
- Func_get_args ();
- Func_get_args ($index);
- Func_num_args ().
PHP functions, Static,globalkeyword and three variable scopes