


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);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的默认参数是10If 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";
相关推荐:
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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.