Home  >  Article  >  Backend Development  >  PHP function declaration and usage

PHP function declaration and usage

大家讲道理
大家讲道理Original
2017-05-28 09:42:411458browse


1. The function name is one of the identifiers. It can only have alphanumeric underscores and cannot start with a number;

Function The naming of the name must comply with the "little camel case rule" FUNC(), func(), Func();

Function names are not case-sensitive;

The function name cannot have the same name as an existing function or the built-in function name;

2. function_exists("func"); Used to detect whether the function has been declared;

NoteThe function name passed in must be in string format, and the result will be returned Is true/false; Scope of variables

]

1. Local variables: Variables declared inside a function are called local variables and are only used inside the function,

If you need to use it outside the function, you need to use the

return keyword in the function to return;

2. Global variables: Variables declared outside the function are called global variables; To use global variables in a function, need to use the global keyword to introduce global variables;

Variable name in the function , if it is repeated with the global variable name, above global, it is the local variable of the function, and below global, it is the global variable of the function;

4.$GLOBALS[''] Globalarray $GLOBALS['a3'] array is a global array built in by PHP. You can directly add values ​​to the array, whether inside or outside the function. Statement,

can be used directly anywhere; eg:

$GLOBALS['a3'] =10;

5. There is another way to use global variables in functions: By passing parameters to parameters, global variables can be used inside the function. But the parameters after passing are local variables and will change internally. , the outside will not change unless the passed parameter is the address .function func($a1,&$a2){}func($a1,$a2);

(Reason) $a1 is a local variable. If it changes internally, it will not change externally. $a2 is also an internal variable address. If it changes internally, it will also change externally;

                                                                                                                                                                      ‐                                                                  

# eg: func($a1,$a2) is right func($a1,2) is wrong

Variable】

Static variable: Use the static keyword to declare, static $num=10;

Static variable Features:

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 is called multiple times and shares the same static variable.

                                                                                                                                                                                                                                             since More than the formal parameters, not less than the formal parameters, otherwise an error will be reported

1. Conventional parameter transfer:

function fun( $a){

$a+=10;

## return $a;

}

## echo fun(10);

2.References parameters of type :

$a=10;

function func(&$a){

$ a+=10;

}func($b);

Reference parameters are passed, variables are modified inside the function, and changes are synchronized outside the function;

The formal parameters are reference parameters, and the actual parameters can only be variables, not literals.

3. Default parameters:

function func($a,$b=10){

return $a+$b;

}

echo func(30); //Default parameters of $b Yes 10

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, the non-default parameter list must be guaranteed. Default parameter assignment order.

func_get_args(); //Get all parameter lists (arrays)

func_num_args (); //Get the total number of all parameters, equivalent to count(func_num_args());

func_get_arg(0); //According to the table below , take each parameter

                          [Variable function]

Convert a function name to a string and assign it to a variable. This variable is what we call a variable function. You can add () to call the function content;

function func(){ }---->fun="func",----->func( );

# [#]

## 1. Use variable function, customize the callback function ;

## Function($func){func();}-->function f(){}--->func("f");

2Use c

all_user_func_array and call_user_func custom callback function;

Two functions The first parameter 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 The parameter list for the callback function is equivalent to apply() in

js; and call_user_func directly expands and writes the parameter list of the callback function into the second and multiple parameters, which is equivalent to js. call();

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);

            Method, $fun()/func() Therefore, in order to make function calls more unified, anonymous functions were created.

The “;” after the function body of the declared anonymous function is essential!!! The anonymous function itself is also a variable, and is detected as object class type by var_dump;

=

Example: Calculate the level of a number:

function jiec($num){    static $jie=1;    //函数执行完不会立即释放
    if($num>0){
        $jie*=$num;   //3
        jiec(--$num);
    }    return $jie;
}
 echo jiec(10);

# [
recursive function

]


指的是在函数内部,调用函数自身的操作;当外层函数体中,遇到自身函数调用,继续进入内层函数执行,而自身函数的后半部分暂不执行,知道最内层函数执行完以后,在逐步向外执行;



function func($num){
     echo $num."
";    if($num>0){         func($num-1);   //func(--$num);   试一试又不一样的结果哟!   //func($num--);    }     echo $num."
";  }func(10);

                                [include/require]
   1.两者的作用就是用于引入外部的PHP文件到当前文件中:include 'a.php';include ('a.php');
   2.两者的区别:(对于错误的处理不同)当引入文件错误时,include会产生警告,并不影响后续代码的执行,而require会产生错误,后续代码不再执行;
   3.一般当用于在文件最上方导入某些文件时,使用require导入,如果失败,则不执行文件;
     如果是在某些分支条件中,导入执行某些操作,一旦报错不影响执行结果。
   4.include_oncerequire_once表示:文件只能导入一次,如果多次调用函数,则后面的文件会判断文件是否导入,再决定是否导入新文件。
     (检测文件是否导入时只关心文件是否已经导入,并不关心使用何种方式导入的。)
   5.include/require可以导入各种类型的文件,相当于在当前文件copy了一份,但是copy过程中,PHP引擎会进行适当的编译,确保不会出错。
   6.include和require是函数也是指令!PHP对于很多常用函数,会提供执行的写法,eg:函数写法echo("111");指令写法echo "111";
  

 

The above is the detailed content of PHP function declaration and usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn