Functions of PHP

Source: Internet
Author: User
Tags types of functions
This article introduces the content of PHP in the function, has a certain reference value, now share to everyone, the need for friends can refer to

Function
What is a function
The code block that completes the specified task and has been named can be used repeatedly.
Functions of the function
Easy to maintain
Code is not redundant, can improve the reusability of code
Types of functions
System functions
Custom functions
To write a custom function
1. Write down the code you want to write
2. Wrap the code in curly braces
3. Declare a function using the name () of the keyword function
4, will need or often become value extracted as a parameter to use
Attention
1. The name of the function must be meaningful
2, the function name can not use a pure number, the beginning can not be used numbers
3. The name of the function is not case-sensitive
4. The name of the function cannot be duplicated, nor can it be duplicated with the name of the system function.
Call to function
Attention
1. function does not call do not execute
2. The function's call can be placed in front of the function declaration, or it can be placed after the function declaration. Because the code is placed in the Code section of the memory, the code is put into memory before the program executes.
Parameters for custom Functions
The custom function design principle, the user can participate in the control custom function, may make some fine tuning to the custom function
Actual parameters
The actual parameters, which are written in the () at the time of the call
Formal parameters
Formal parameters, arguments provided when declaring parameters
Default parameters
If the specified number of actual attendance errors are not filled in when the function is used, we can give the parameter default values while declaring the parameters
Attention
1. When there is a default value in the formal parameter, and there is a value in the argument, the value in the argument overrides the parameter's value
2, can be given partial parameters, some parameters have default values, but need to be placed in the back as far as possible, because the parameter value is one by one corresponding
3, the function does not give the parameter default value called the required parameter
4. Parameters with default values in the function are called optional parameters
Variable-length parameter list
Func_get_args () Gets a collection of all the actual arguments in the function
Func_num_args () Gets the number of actual arguments in the function
Func_get_arg (number of parameters) to obtain the value of the first few parameters
Attention
1. The first few parameters are starting from 0, not starting from 1.
2. There is no error in the number of parameters in the function, but it is ignored by default. We can use system functions to get relevant information
Reference parameters
Application Scenarios
Typically used in cases where you want to modify the parameter value itself
Attention
1, when using reference parameters,& to be placed on the formal parameter
2, if it is a reference parameter, the argument must use the variable cannot use the value, because only the variable has the address
Return value in a custom function return
We sometimes need to process the result of the function again, in order to solve this problem we need to use return
Format
Return the value you want returned
Attention
1, return value, return to where the function is called
2, return returns the value after return, the code after return does not execute
3. In general, we will not use echo more than once when we write the function, but return the value
Variables are categorized by scope
1. Local Variables
Attention
1, the function declaration of the variable can only be called within the function, this is the local variable
2, the function of the formal parameter, is actually a local variable, the call when the actual parameters passed to the inside of the function
2. Global variables
Variables declared outside the function we call global variables, can be used in functions (conditionally, you need to add the Global keyword)
The global variable name can use external variables inside the function
The ligatures Way of global
global variable name 1, variable name 2, ....
3. Super Global variables
$GLOBALS refer to all variables in the global scope, which is created automatically, he contains all the global variables and needs to use the $globals[' variable name '
The difference between $GLOBALS and global
1, variables declared outside the function we call global variables, can be used in the function (conditionally, you need to add the Global keyword)
2, $GLOBALS [] is called an external variable, and inside and outside of the function will remain consistent.
static variables
Format
Static variable name
Attention
1. Static local variables are initialized only once
2. A static property can only be initialized to a character value or to a constant or to an expression
3. Does not change as function calls and exits
Variable function
function to run the value of a variable (string) as the name of the function in parentheses

<?php    function Say () {        echo ' this is say ';    }    $var = ' say ';    $var ();? >

callback function
The callback function is actually the function name is passed as a string and then use the variable function to run the way
Recursive functions
Recursive function: Call yourself yourself
Attention
1, you yourself is this function, you call yourself, when you finish one thing, you will do the last thing you did not finish
2, recursive function must have a termination condition, otherwise will be a dead loop
File loading
Include
Format
Include (' file path to include ')
Attention:
There are functions in the system that are not functions, it is a system command you can also write include ""
Include_once
Contains only once the file, he will check whether it has been included, if included he will not contain again, if not included he will contain
Require
Require (' file path to include ')
Require_once
It is also checked for inclusion, and if it is included, it is no longer included
Attention
Description: Include and require the functions of the two, but not the alias of the relationship
The difference between include and require
Require: If it contains an error, it will cause a fatal error.
Include: A warning will be generated if the error is included, and the following code will continue to execute
Special attention
If you are missing a file that you want to include, use require when the program is not running, instead use the Include
What is a function
The code block that completes the specified task and has been named can be used repeatedly.
Functions of the function
Easy to maintain
Code is not redundant, can improve the reusability of code
Types of functions
System functions
Custom functions
To write a custom function
1. Write down the code you want to write
2. Wrap the code in curly braces
3. Declare a function using the name () of the keyword function
4, will need or often become value extracted as a parameter to use
Attention
1. The name of the function must be meaningful
2, the function name can not use a pure number, the beginning can not be used numbers
3. The name of the function is not case-sensitive
4. The name of the function cannot be duplicated, nor can it be duplicated with the name of the system function.
Call to function
Attention
1. function does not call do not execute
2. The function's call can be placed in front of the function declaration, or it can be placed after the function declaration. Because the code is placed in the Code section of the memory, the code is put into memory before the program executes.
Parameters for custom Functions
The custom function design principle, the user can participate in the control custom function, may make some fine tuning to the custom function
Actual parameters
The actual parameters, which are written in the () at the time of the call
Formal parameters
Formal parameters, arguments provided when declaring parameters
Default parameters
If the specified number of actual attendance errors are not filled in when the function is used, we can give the parameter default values while declaring the parameters
Attention
1. When there is a default value in the formal parameter, and there is a value in the argument, the value in the argument overrides the parameter's value
2, can be given partial parameters, some parameters have default values, but need to be placed in the back as far as possible, because the parameter value is one by one corresponding
3, the function does not give the parameter default value called the required parameter
4. Parameters with default values in the function are called optional parameters
Variable-length parameter list
Func_get_args () Gets a collection of all the actual arguments in the function
Func_num_args () Gets the number of actual arguments in the function
Func_get_arg (number of parameters) to obtain the value of the first few parameters
Attention
1. The first few parameters are starting from 0, not starting from 1.
2. There is no error in the number of parameters in the function, but it is ignored by default. We can use system functions to get relevant information
Reference parameters
Application Scenarios
Typically used in cases where you want to modify the parameter value itself
Attention
1, when using reference parameters,& to be placed on the formal parameter
2, if it is a reference parameter, the argument must use the variable cannot use the value, because only the variable has the address
Return value in a custom function return
We sometimes need to process the result of the function again, in order to solve this problem we need to use return
Format
Return the value you want returned
Attention
1, return value, return to where the function is called
2, return returns the value after return, the code after return does not execute
3. In general, we will not use echo more than once when we write the function, but return the value
Variables are categorized by scope
1. Local Variables
Attention
1, the function declaration of the variable can only be called within the function, this is the local variable
2, the function of the formal parameter, is actually a local variable, the call when the actual parameters passed to the inside of the function
2. Global variables
Variables declared outside the function we call global variables, can be used in functions (conditionally, you need to add the Global keyword)
The global variable name can use external variables inside the function
The ligatures Way of global
global variable name 1, variable name 2, ....
3. Super Global variables
$GLOBALS refer to all variables in the global scope, which is created automatically, he contains all the global variables and needs to use the $globals[' variable name '
The difference between $GLOBALS and global
1, variables declared outside the function we call global variables, can be used in the function (conditionally, you need to add the Global keyword)
2, $GLOBALS [] is called an external variable, and inside and outside of the function will remain consistent.
static variables
Format
Static variable name
Attention
1. Static local variables are initialized only once
2. A static property can only be initialized to a character value or to a constant or to an expression
3. Does not change as function calls and exits
Variable function
function to run the value of a variable (string) as the name of the function in parentheses

<?php                                        function Say () {                                        echo ' this is say ';                                    }                                            $var = ' say ';                                            $var ();                                    ? >

callback function
The callback function is actually the function name is passed as a string and then use the variable function to run the way
Recursive functions
Recursive function: Call yourself yourself
Attention
1, you yourself is this function, you call yourself, when you finish one thing, you will do the last thing you did not finish
2, recursive function must have a termination condition, otherwise will be a dead loop
File loading
Include
Format
Include (' file path to include ')
Attention:
There are functions in the system that are not functions, it is a system command you can also write include ""
Include_once
Contains only once the file, he will check whether it has been included, if included he will not contain again, if not included he will contain
Require
Require (' file path to include ')
Require_once
It is also checked for inclusion, and if it is included, it is no longer included
Attention
Description: Include and require the functions of the two, but not the alias of the relationship
The difference between include and require
Require: If it contains an error, it will cause a fatal error.
Include: A warning will be generated if the error is included, and the following code will continue to execute
Special attention
If you are missing a file that you want to include, use require when the program is not running, instead use the Include

Related recommendations:

Explore PHP's function running mechanism _php tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.