Home >Backend Development >PHP Tutorial >Benniao learns PHP (5) Declaration and use of functions_PHP tutorial
1. Function declaration
<?php /** * function 函数名(参数1, 参数2.... ){ * 函数体; * 返回值; * } */ $sum = sum(3, 4); echo $sum; function sum($x, $y){ $sum = 0; $sum = $x * $x + $y + $y; return $sum; } ?>
2. Scope of variables
Local variables: Variables declared in a function are local variables and can only be used within your own function .
Global variables: Variables declared outside a function are global variables. They can be used after the variable is declared until the end of the entire script, including in functions and { }.
2.1 Knowledge point: PHP variables cannot be distinguished whether they are declared or used
<?php $a = 10; function demo($a){ $a += 10; echo $a . '<br>'; } demo($a); echo $a; // 结果: 20, 10 // PHP的变量分不出 $a 是声明还是使用 ?>2.2 Knowledge point: When using global variables in a function, you must use the global keyword. The variable after declaring global is the global variable
<?php $a = 10; function demo() { global $a; $a += 10; echo $a . " demo <br>"; } function test() { global $a; $a += 5; echo $a . "test <br>"; } echo $a . " ----<br>"; // 10 ---- demo(); // 20 demo echo $a . " !!!!<br>"; // 20 !!!! demo(); // 30 demo echo $a . " @@@@<br>"; // 30 @@@@ test(); // 35 test echo $a . " ####<br>"; // 35 #### ?>
Static variables: can only be declared in functions (classes) and cannot be declared globally. Use the static keyword to modify before the variable.
2.3 Knowledge point: Static variables are stored in static code blocks. Its value can be shared between multiple calls of a function, but it is only declared to memory when the function is called for the first time
When called in the future, it will not be declared but used directly (similar to Java).
<?php function test(){ static $a = 0; $a++; echo $a."<br>"; } test(); // 1 test(); // 2 test(); // 3 ?>
3. Variable function
In fact, this is very similar to JS, for example: define $var = hello, the next time you use $var(), you will look for the function hello() with the same name as the variable value
<?php function one($a, $b){ return $a + $b; } function two($a, $b){ return $a * $a + $b * $b; } function three($a, $b){ return $a * $a * $a + $b * $b * $b; } //$var = one; //$var = "two"; $var = three; echo "结果:".$var(3, 4)."<br>"; // 91 ?>
4. System function
4.1 Regular functions4.3 Function with & parameter, indicating reference assignment. This parameter cannot pass a value, only a variable can be passed, and then the function changes the value of the variable,
When we use this variable, the value also changes (the same as passing references in Java)
bool arsort (array &array [, int sort_flags])<?php $arr=array(1, 9, 5, 8, 3, 4); sort($arr); print_r($arr); ?>4.4 Default function, a function with [], indicates that this parameter is optional. If you pass a value, the value you passed will be used. If no value is passed, the default value will be used
<?php function demo($a=1, $b=20, $c){ echo "### $a ### $b ### $c ###<br>"; } demo(8,9); ?>4.5 Parameter function with..., ... means that any number of parameters can be passed
<?php function demo(){ $sum = 0; for($i = 0; $i < func_num_args(); $i++){ $sum += func_get_arg($i); } return $sum; } echo demo(1, 2, 3, 4, 5, 6, 7, 8, 9); ?>
4.6 Callback function with callback, that is, when calling this function, we need to pass in a function (function name, function name string)
array array_filter (array input [, callback callback])
<?php function demo($n){ if($n % 2== 0){ return true; }else{ return false; } } $a = array(1, 2, -3, 4, -5, 6, -7, 8, 9); print_r(array_filter($a, demo)); // Array ( [1] => 2 [3] => 4 [5] => 6 [7] => 8 ) ?>