


Detailed graphic explanation of function declaration and usage in PHP
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 reported1. 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);
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
[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";
相关推荐:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
