Home  >  Article  >  Backend Development  >  Detailed graphic explanation of function declaration and usage in PHP

Detailed graphic explanation of function declaration and usage in PHP

墨辰丷
墨辰丷Original
2018-05-22 10:31:311291browse

This article mainly introduces the function declaration and use in PHP. Friends who need it can refer to it

Function

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

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

Function names are not case-sensitive;

Function names cannot have the same name as existing functions or built-in function names;

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

Note that the function name passed in must be in string format, and the return result is true/false;

Echo When printing, True is 1, false does not display;

## [The role of variables in pHP] # 1. Local variables : Variables declared inside a function are called local variables and are only used inside the function. If they need to be used outside the function, they need to use the return keyword in the function;

2. Global variables: declared outside the function Variables are called global variables;

3. (More commonly used) Variables used in functions use local variables by default. If you need to use global variables in a function, you need to use the global keyword to introduce global variables;

If the variable name in the function 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['' ] Global array;

$GLOBALS['a3'] array is a global array built in by PHP. Values ​​can be added directly to the array. Whether declared inside or outside the function, it 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 change internally and will not change externally, unless the parameter passed is an address. function func($a1,&$a2){}func($a1,$a2);

(reason) $a1 It 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;

If the address symbol appears in the formal parameter of the function, when the function is called, the Participating must be variables, not the literal amount;

Eg: Func ($ A1, $ A2) The Func ($ A1, 2) Wrong

# [Static variables]

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

Characteristics of static variables:

Static variables Declared when the function is loaded for the first time;

The static variables will not be released immediately after the function is used, and the static variables will only be declared once during the entire script execution;

The same function multiple times Called, share the same static variable.

                                                                                                                                                                                                                                                         Less, otherwise an error will be reported

1. Conventional parameter transfer:

  function fun($a){
  $a+=10;
  return $a;
  }
 echo fun(10);

2. Reference type Parameters:

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

Formal parameters are reference parameters, Actual parameters can only be variables, not literals.

3. Default parameters:

  function func($a,$b=10){
  return $a+$b;
  }
  echo func(30);  //$b的默认参数是10

If there are both default parameters and non-default parameters in the parameters, Then, the default parameter list must follow the non-default parameter list, that is, the order of assignment of non-default parameters must be guaranteed.                                                           

[Variable function]

  将一个函数名,转为字符串后,赋给一个变量。这个变量,就是我们所说的变量函数,可以加()调用函数内容;
                function func(){ }---->fun="func",----->func( );

                                 [回调函数]

   1.使用变量函数,自定义回调函数

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

   2使用call_user_func_array和call_user_func自定义回调函数;

     两个函数的第一个参数,均为回调函数,表示执行当前回调;

     不同点在于:call_user_func_array()第二个参数为数组,并将数组的每一个值赋给回调函数的参数列表,相当于js中的apply(); 而,call_user_func,是将回调函数的参数列表,直接展开写到第2-多个参数中,相当于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);

                                [ 匿名函数]   

    由于变量函数在调用时存在多种调用方式,$fun()/func()所以为了让函数的调用更为统一,才产生了匿名函数。
    声明匿名函数函数体后面的;必不可少!!!

    匿名函数本身也是变量,用var_dump检测为object类型;

常规函数:

function func(){
   $fun="func"
 }
 $fun();//func();

匿名函数:

$func=function($a){
 echo "我是匿名函数{$a}<br/>";
  };    //声明匿名函数函数体后面的;必不可少
 $func(10);
 var_dump($func);

例题:计算一个数的阶层:

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

                                [递归函数]

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

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

                                [include/require]

   1.两者的作用就是用于引入外部的PHP文件到当前文件中:include 'a.php';include ('a.php');

   2.两者的区别:(对于错误的处理不同)当引入文件错误时,include会产生警告,并不影响后续代码的执行,而require会产生错误,后续代码不再执行;

   3.一般当用于在文件最上方导入某些文件时,使用require导入,如果失败,则不执行文件;

     如果是在某些分支条件中,导入执行某些操作,一旦报错不影响执行结果。

   4.include_once和require_once表示:文件只能导入一次,如果多次调用函数,则后面的文件会判断文件是否导入,再决定是否导入新文件。

     (检测文件是否导入时只关心文件是否已经导入,并不关心使用何种方式导入的。)

   5.include/require可以导入各种类型的文件,相当于在当前文件copy了一份,但是copy过程中,PHP引擎会进行适当的编译,确保不会出错。

   6.include和require是函数也是指令!PHP对于很多常用函数,会提供执行的写法,eg:函数写法echo("111");指令写echo "111";

相关推荐:

函数声明与函数表达式有什么区别

php的闭包和匿名函数声明实例详解

php自定义函数声明,调用,参数和返回值基础教程

The above is the detailed content of Detailed graphic explanation of function declaration and usage in PHP. 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