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 thereturn 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: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 wrongVariable】
Static variable: Use the static keyword to declare, static $num=10;
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."<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 PHP function declaration and usage. 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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft