The content introduced in this article is about functions in php, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Function
What is a function
A named block of code that completes a specified task and can be used repeatedly.
The role of function
Easy to maintain
The code is not redundant and can improve the reusability of the code
Types of functions
System functions
Custom functions
Steps to write custom functions
1. Write out the code you want to write
2. Wrap the code in braces
3. Use the keyword function () To declare the function
4. Extract the required or frequently changed value as a parameter
Note
1. The name of the function It must be meaningful
2. Function names cannot use pure numbers, and numbers cannot be used at the beginning
3. Function names are not case-sensitive
4. The name of the function cannot be repeated, nor can it be repeated with the custom one or the name of the system function
Call of the function
Note
1. The function will not be executed if it is not called.
2. The function call can be placed before the function declaration or after the function declaration. Because the code is placed in the code section of the memory, the code has been placed in the memory before the program is executed.
Parameters of custom functions
Since Define the design principle of the function. Users can participate in controlling the custom function and make some fine-tuning of the custom function
actual parameters
actual parameters when calling Parameters written in ()
Formal parameters
Formal parameters, when declaring parameters, provided parameters
Default parameters
If you do not fill in the specified number of actual parameters when using a function, an error will be reported, but we can give the formal parameters a default value while declaring the formal parameters
Note
1. When there is a default value in the formal parameter and there is a value in the actual parameter, the value in the actual parameter will overwrite the value of the formal parameter.
2. Yes Given some parameters, some parameters have default values, but they need to be placed as late as possible, because the parameter values are in one-to-one correspondence
3. Parameters that do not give default values in the function are called required parameters.
4. Parameters with default values in the function are called optional parameters
Variable length parameter list
func_get_args() Get the function The collection of all actual parameters
func_num_args() Gets the number of actual parameters in the function
func_get_arg (the number of parameters) Gets the value of the number of parameters
Note
1. Which parameter starts from 0 and not from 1
2. The actual parameter format in the function It won't go wrong if there are too many parameters, but they will be ignored by default. We can use system functions to obtain relevant information
Reference parameters
Application scenarios
Usually used when the parameter value itself needs to be modified Next
Note
1. When using reference parameters, & should be placed on the formal parameter
2. If it is a reference parameter, pass Variables must be used when parameters are used, and values cannot be used, because only variables have addresses
The return value in the custom function return
We sometimes need to perform the function on the result Process again, in order to solve this problem we need to use the return
format
return the value you want to return
Note
1. The return value of return will be returned wherever the function is called.
2. After the value returned by return is returned, the code after return will not be executed
3. Under normal circumstances, when we write a function, we will not use echo multiple times, but Is to return the value
Variables are classified according to scope
1. Local variables
Note
1. The variables declared by the function can only be called within the function. This is the local variable
2. The formal parameter of the function is actually a local variable. When calling, the actual parameters are passed to the inside of the function
2. Global variables
Variables declared outside the function are called global variables and can be used in the function (conditionally, you need to add the global keyword)
global variable name You can use external variables inside the function using the
global syntax
global Variable name 1, variable name 2 ,......
3. Super global variables
$GLOBALS refers to all variables in the global scope. This variable is automatically created. Contains all global variables, you need to use $GLOBALS['variable name'] to use
The difference between $GLOBALS and global
1. Outside the function The declared variables are called global variables and can be used in functions (conditionally, you need to add the global keyword)
2. $GLOBALS[] calls external variables and will remain inside and outside the function. consistent.
Static variable
Format
static variable name
Note
1. Static local variables will only be initialized once
2. Static properties can only be initialized to a character value or a constant or an expression
3 , will not change as the function is called and exits
Variable function
Run the value of the variable (string) as the name of the function plus parentheses The function
<?php function say(){ echo '这个是say'; } $var = 'say'; $var(); ?>
Callback function
The callback function actually passes the function name in the form of a string and then runs it using a variable function
Recursive function
Recursive function: call yourself
Note
1. You are this function, you are yourself Call yourself. When you finish something, you will do what you didn't finish last time.
2. The recursive function must have a termination condition, otherwise it will enter an infinite loop
File loading
include
Format
include('File path to include')
Note:
There are some functions and non-functions in the system. They are system commands. You can also write include ""
include_once
Include the file only once. It will check whether it is already included. If it is included, it will not include it again. If it is not included, it will only include it.
require
require('File path to be included')
require_once
also checks whether it is included. If it is included, it will no longer be included
Note
Note: include and require have similar functions, but they are not related to aliases
The difference between include and require
require: If an error is included, a fatal error will be generated
include: If an error is included, a warning will be generated, and the following code will continue to execute
Special attention
If you are missing the file you want to include and the program cannot run, use require, otherwise use include
What is a function
The named code block that completes the specified task can be used repeatedly.
The role of functions
Easy to maintain
The code is not redundant and can improve the reusability of the code
Types of functions
System function
Custom function
Steps to write a custom function
1. Write out the code you want to write
2. Wrap the code in curly brackets
3. Use the keyword function function name () to declare the function
4. Extract the required or frequently required values and use them as parameters
Note
1. The name of the function must be meaningful
2. Function names cannot use pure numbers, and numbers cannot be used at the beginning
3. Function names are not case-sensitive
4. Function names cannot be repeated. It cannot be repeated with the name of the customized one or the name of the system function
Call of the function
Note
1. If the function is not called, no Execution
2. The function call can be placed before the function declaration or after the function declaration. Because the code is placed in the code section of the memory, the code has been placed in the memory before the program is executed.
Parameters of custom functions
Since Define the design principle of the function. Users can participate in controlling the custom function and make some fine-tuning of the custom function
actual parameters
actual parameters when calling Parameters written in ()
Formal parameters
Formal parameters, when declaring parameters, provided parameters
Default parameters
If you do not fill in the specified number of actual parameters when using a function, an error will be reported, but we can give the formal parameters a default value while declaring the formal parameters
Note
1. When there is a default value in the formal parameter and there is a value in the actual parameter, the value in the actual parameter will overwrite the value of the formal parameter.
2. It can be given Some parameters have default values, but they need to be placed as late as possible, because the parameter values are in one-to-one correspondence
3. Parameters that do not give default values in the function are called required parameters
4. Parameters with default values in the function are called optional parameters
Variable length parameter list
func_get_args() Gets all the actual parameters in the function Set of parameters
func_num_args() Get the number of actual parameters in the function
func_get_arg (the number of parameters) Get the value of the number of parameters
Note
1. Which parameter starts from 0, not 1
2. There are more actual parameters than formal parameters in the function It won't go wrong if the number is large, but it will be ignored by default. We can use system functions to obtain relevant information
Reference parameters
Application scenarios
Usually used when the parameter value itself needs to be modified Next
Note
1. When using reference parameters, & should be placed on the formal parameter
2. If it is a reference parameter, pass Variables must be used when parameters are used, and values cannot be used, because only variables have addresses
The return value in the custom function return
We sometimes need to perform the function on the result Process again, in order to solve this problem we need to use the return
format
return the value you want to return
Note
1. The return value of return will be returned wherever the function is called.
2. After the value returned by return is returned, the code after return will not be executed.
3. Under normal circumstances, when we write a function, we will not use echo multiple times, but return the value
Variables are classified according to scope
1. Local variables
Note
1. Variables declared by a function can only be called within the function. This is a local variable
2. The formal parameter of a function is actually a local variable. When calling, the actual parameters are passed to the inside of the function
2. The global variable
is declared outside the function Variables are called global variables and can be used in functions (conditionally, you need to add the global keyword)
global variable name External variables can be used inside the function
Global concatenation method
global Variable name 1, variable name 2,......
3, super global variable
$GLOBALS refers to all variables in the global scope. This variable is automatically created. It contains all global variables. You need to use $GLOBALS['variable name'] to use it
$The difference between GLOBALS and global
1. Variables declared outside the function are called global variables and can be used in the function (conditionally, you need to add the global keyword)
2. $GLOBALS[] calls external variables and will remain consistent inside and outside the function.
Static variable
Format
static variable name
Note
1. Static local variables will only be initialized once
2. Static properties can only be initialized to a character value or a constant or an expression
3 , will not change as the function is called and exits
Variable function
Run the value of the variable (string) as the name of the function plus parentheses The function
<?php function say(){ echo '这个是say'; } $var = 'say'; $var(); ?>
回调函数
回调函数实际上就是将函数名以字符串的形式传递然后使用变量函数的方式来运行的
递归函数
递归函数:自己调用自己
注意
1、你自己就是这个函数,是你自己调用自己,当自己做完一件事情后,你会做上次你没做完的事情
2、递归函数一定要有一个终止条件,否则将进行死循环
文件加载
include
格式
include('要包含的文件路径')
注意:
系统里面有些函数非函数,它是系统命令你还可以写成 include “”
include_once
只包含一次该文件,他会检查是否已经包含了,如果包含了他就不再次包含,如果没有包含他才会包含
require
require('要包含的文件路径')
require_once
也是检查是否包含,如果包含了就不再包含
注意
说明: include和require他俩的功能差不多,但不是别名的关系
include和require的区别
require:如果包含错误的话,将会产生一个致命的错误
include:如果包含错误的话将会产生一个警告,下面的代码还会继续执行
特别注意
如果,你缺少了你要包含的文件程序运行不下去的时候使用require,反之使用include
相关推荐:
The above is the detailed content of php function. For more information, please follow other related articles on the PHP Chinese website!

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.