Home > Article > Backend Development > The introductory knowledge you must master in PHP
***Keywords in this chapter: Declaration and use of functions; Scope of variables in PHP; Static variable; function parameter transfer; variable function; callback function; anonymous function; include&require; PHP closure
Now, let’s take a look at the basics of PHP functions ↓↓↓
1. The function name is one of the identifiers. It can only have numbers, letters, and underscores, and cannot start with numbers.
The naming of function names must comply with the "little camel case rule": myNameIsXiaoxi my_name_is_xiaoxi
Function names are not case-sensitive. func() Func() FUNC() are all OK
The function name cannot be the same as the existing function name or the system function name.
2. function_exists("func1"); used to detect whether the function has been declared;
Note: The function name passed in must be a string Format: The return result is true/false.
1. Local variables: Variables declared inside a function are called local variables. It can only be used inside the function. It needs to be used outside the function. You need to use the return keyword in the function to return.
2. Global variables: Variables outside the declared function are called global variables.
3. In functions, variables are used, and internal local variables are used by default. If you need to use global variables in a function, you need to use the global keyword and reference the global variable to the function before it can be used.
If the variable name in the function is repeated with the global variable, the name above the global statement is the function local variable; below the global statement, it is the system global variable.
4. $GLOBALS[] global array:
$GLOBALS[] array is a global array built in by PHP. You can add it directly to the array. Values, whether declared inside or outside a function, can be used directly anywhere.
$GLOBALS["a"] = 10; ---> echo $GLOBALS["a"];//Use
## directly at any position #5. There is another way to use global variables in functions: by passing parameters to the function, global variables can be used inside the function;
However, the parameters passed after they are passed are local variables of the function. Change, the outside will not change.
Unless the parameter passed is the address function func($a1,&$a2){} //If a1 is modified internally in the function, the global a1 will not change. If a2 is modified internally in the function, the global a2 will be unchanged. Will change.
If the formal parameter of the function contains an address symbol, then when the function is called, the actual parameter must be a variable, not a literal.
For example, in the above example, func($a1,$a2); √ func($a1,2); ×
6. Use require and include does not affect scope.
1. Static variables are declared using the static keyword. static $num = 10;
2. Characteristics of static variables:
>>>Static variables are declared when the function is first loaded.
>>>Static variables will not be released immediately after the function is used. Static variables will only be declared once during the entire script execution process.
>>>The same function, called multiple times, shares the same static variable.
1. In PHP, when it comes to parameter passing: the actual parameter list can only be more than the formal parameters.
2. Conventional parameter passing: function func($a){} func($ a);
3. Passing reference parameters: function func(&$a){} func($a);
① Passing reference parameters through &, function Modify variables internally and change synchronously outside the function
②Formal parameters are reference parameters, actual parameters can only be variables, not literals func(10); ×
4. Default parameters: function func($b,$a=10){} func(20);
Undefined error when used.
If there are both default parameters and non-default parameters in the parameters, then the default parameter list
must be after the non-default parameter list, that is, it must be ensured when calling Prioritization of non-default lists.
5. Variable parameter list: Since PHP can have more actual parameters than formal parameters, we can pass N actual parameters and get the corresponding parameters through PHP built-in functions.
var_dump(func_get_args());//Get all parameter lists (arrays)
var_dump(func_num_args());//The total number of returned parameters is equivalent In count(func_get_args());
var_dump(func_get_arg(0));//Return each parameter according to the subscript
1. After converting a function name to a string, assign Give a variable. This variable is what we call a variable function, and you can add () to call the function content.
function func(){}--->$fun = "func";--->$func();
##1. Use variable function to customize callback function
function func($func){$func();}--->function f(){}--->func("f");
2. Use call_user_func_array and call_user_func to customize the callback function;
The first parameter of the two functions is a callback function, indicating the execution of the current callback.
The difference is: the second parameter of call_user_func_array() is an array, and each value of the array is assigned to the parameter list of the callback function, which is equivalent to apply() in JS;
And call_user_func is to directly expand and write the parameter list of the callback function into the second ~ multiple parameters, which is equivalent to call() in JS;
eg:call_user_func_array ("func",array(1,2,3));
--> func(1,2,3);
call_user_func("func ",1,2,3); --> func(1,2,3);
Due to the variable function, there are multiple callers (func()/$func();) when calling
So in order to make the function calls more uniform, anonymous functions were created! ! !
Declaring an anonymous function, the semicolon after the function body is essential!
The anonymous function itself is also a variable, and is detected as an Object type using var_dump();
Demo code:
<span style="font-family: 楷体; font-size: 14pt"><span style="color: #008080"> 1</span> <span style="color: #0000ff">function</span><span style="color: #000000"> func(){}</span><span style="color: #008080"> 2</span> <span style="color: #800080">$fun</span> = "func"<span style="color: #000000">;</span><span style="color: #008080"> 3</span> <span style="color: #800080">$fun</span><span style="color: #000000">();</span><span style="color: #008080"> 4</span> <span style="color: #000000"> func();</span><span style="color: #008080"> 5</span> <span style="color: #008080"> 6</span> <span style="color: #800080">$func</span> = <span style="color: #0000ff">function</span><span style="color: #000000">(){</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">echo</span> "I am ZXX<br />"<span style="color: #000000">;</span><span style="color: #008080"> 8</span> <span style="color: #000000">};</span><span style="color: #008080"> 9</span> <span style="color: #800080">$func</span>(10<span style="color: #000000">);</span><span style="color: #008080">10</span> <span style="color: #008080">var_dump</span>(<span style="color: #800080">$func</span>);<br></span>
1、两者的作用,都是用于引入外部的PHP文件到当前文件中。
2、两者的区别:当引用文件错误时,include会产生警告,并不影响后续代码执行。
而require会产生致命错误,后续代码全部不再执行。
3、一般,当用于在文件最上方导入某些文件时,使用require导入,如果导入失败,则不再执行文件;
而如果是在某些分支条件中,导入执行某些操作,则一般使用include。一旦报错,不影响其他分支功能。
4、include_once和require_once表示:文件只能导入一次,如果多次调用此函数,则后面的语句会判断文件是否导入。
再决定是否导入新文件。
(检测文件是否导入时,只关心文件是否已经导入,并不关心是否以何种方式导入的)
include("test.txt");
require_once "test.txt";//由于include进来了,require不再导入。
5、include和require可以导入各种类型的文件。
相当于在当前文件copy了一份,但是copy过程中,PHP引擎会进行适当的编译,确保不会出错。
6、include和require是函数,也是指令!PHP对于很多常用函数会提供指令的写法。
eg:echo("11");//函数写法 echo"11";//指令写法
演示代码:
1 $num = true;2 if($num){3 include'functionDate.php';4 //require "functionDate.php";5 func1();6 func2();7 func3();8 }9 echo "haha";
在PHP中,子函数无法直接访问父函数的局部变量,必须使用User关键词向下传递!!!
1 $a = 12; 2 function func1(){ 3 $num = 10; 4 $num1 = 11; 5 $func2 = function() use($num,$num1){//使用use关键词传递父函数局部变量 6 global $a; 7 var_dump($a); 8 var_dump($num); 9 var_dump($num1);10 };11 return $func2;12 }13 $f = func1();14 $f($num);
The above is the detailed content of The introductory knowledge you must master in PHP. For more information, please follow other related articles on the PHP Chinese website!