php function

Apr 23, 2018 am 10:42 AM
php function

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 &#39;这个是say&#39;;
    }
    $var = &#39;say&#39;;
    $var();
?>
Copy after login

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 &#39;这个是say&#39;;
                                    }
                                            $var = &#39;say&#39;;
                                            $var();
                                    ?>
Copy after login

回调函数
回调函数实际上就是将函数名以字符串的形式传递然后使用变量函数的方式来运行的
递归函数
递归函数:自己调用自己
注意
1、你自己就是这个函数,是你自己调用自己,当自己做完一件事情后,你会做上次你没做完的事情
2、递归函数一定要有一个终止条件,否则将进行死循环
文件加载
include
格式
include('要包含的文件路径')
注意:
系统里面有些函数非函数,它是系统命令你还可以写成 include “”
include_once
只包含一次该文件,他会检查是否已经包含了,如果包含了他就不再次包含,如果没有包含他才会包含
require
require('要包含的文件路径')
require_once
也是检查是否包含,如果包含了就不再包含
注意
说明:   include和require他俩的功能差不多,但不是别名的关系
include和require的区别
require:如果包含错误的话,将会产生一个致命的错误
include:如果包含错误的话将会产生一个警告,下面的代码还会继续执行
特别注意
如果,你缺少了你要包含的文件程序运行不下去的时候使用require,反之使用include

相关推荐:

探究PHP的函数运行机制_PHP教程

The above is the detailed content of php function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles