Php basics (3) --- PHP function basics, php --- php

Source: Internet
Author: User
Tags php basics

Php basics (3) --- PHP function basics, php --- php
Php basics (3)-Today I will share with you the basics of PHP functions. With the first two chapters, I think you have a basic understanding of PHP. If you want to review the previous two chapters, you can click "php basics (1)" and "php basics (2)" to go to the page and learn about the basics of PHP basics.

* ** Keywords in this chapter: Declaration and use of functions; Scope of variables in PHP; static variables; parameter transfer of functions; variable functions; callback functions; anonymous functions; include & require; PHP Closure

 

Now, let's take a look at the PHP function basics.

 

 

 

 

I Function declaration and use

1. The function name is one of the identifiers. It can only contain numbers, letters, and underscores. It cannot start with a number.
The name of the function must comply with the "small hump rule": myNameIsXiaoxi my_name_is_xiaoxi
The function name is case insensitive. Func () Func () FUNC () can all be
The function name cannot be the same as the existing function name.

2. function_exists ("func1"); used to check whether the function has been declared;
Note: The input function name must be in string format: the returned result is true/false.

 

II Scope of variables in PHP

1. Local variables: variables declared inside the function are called local variables. This function can be used only within the function, and must be used in addition to the function. You must use the return keyword to return the function.
2. Global variables: the external variables of declared functions are called global variables.
3. Use variables in the function. Internal local variables are used by default. If you want to use global variables in a function, you must use the global keyword to reference the global variables.

The variable name in the function. If it is the same as the global variable, the global statement above the global statement is the local variable of the function; the global statement is the global variable of the system.

4. $ GLOBALS [] global array:
$ GLOBALS [] array is a global array built in PHP. You can directly add values to the array, which can be directly used anywhere, Whether Declared inside or outside the function.
$ GLOBALS ["a"] = 10; ---> echo $ GLOBALS ["a"]; // directly use

5. global variables are used in functions. You can also use global variables in functions by passing parameters to functions;
However, after passing the parameter, it is the local variable of the function. The parameter changes internally and the external variable does not change.
Unless the passed parameter is the address function func ($ a1, & $ a2) {} // you can modify a1 in the function. Then, the global a1 will not change. The function modifies a2 in the function, then global a2 will change.

If the parameter of a function has an address-taking symbol, the real parameter must be a variable rather than a literal value when calling the function.
In the preceding example, func ($ a1, $ a2); √ func ($ a1, 2); ×

6. Using require and include does not affect the scope.

 

3. Static variables

1. static variables are declared using the static keyword. Static $ num = 10;
2. Static variables:
>>> Static variables are declared when the function is loaded for the first time.
>>> Static variables are not released immediately after the function is used. Static variables are declared only once during script execution.
>>> The same function is called multiple times and the same static variable is shared.

 

Thu Function parameter transfer

1. in PHP, when parameter passing is involved: the real parameter list can only be more than the actual parameter list,
2. Pass common parameters: function func ($ a) {} func ($ );
3. Pass the reference parameter: function func (& $ a) {} func ($ );
① Pass through & reference parameters, modify variables in the function, and synchronize changes outside the Function
② The parameter is a reference parameter. The real parameter can only be a variable, not a literal func (10). ×
4. default parameter: function func ($ B, $ a = 10) {} func (20 );
// Yes $. The default value is 10. $ B must be passed. Otherwise, an error is returned.
If there are both default parameters and non-default parameters, the default parameter list
It must be placed behind the non-default parameter list, that is, the priority value of the non-default list must be ensured during the call.

5. Variable Parameter List: Because PHP real parameters can be more than the form parameters, we can pass N real parameters and get the corresponding parameters through the PHP built-in functions.
Var_dump (func_get_args (); // retrieve the list of all parameters (array)
Var_dump (func_num_args (); // The total number of returned parameters is equivalent to count (func_get_args ());
Var_dump (func_get_arg (0); // return each parameter based on the subscript

 

V. Variable Functions

1. convert a function name into 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 ();

 

Sat. Callback Function

1. Use variable functions to customize callback Functions
Function func ($ func) {$ func () ;}---> function f () {}---> func ("f ");

2. Use call_user_func_array and call_user_func to customize the callback function;
The first parameter of both functions is the callback function, which indicates that the current callback is executed.
The difference is: call_user_func_array () The second parameter is an array, and each value of the array is assigned to the callback function parameter list, which is equivalent to apply () in JS ();
Call_user_func refers to the callback function parameter list, which is directly expanded to 2nd ~ Among multiple parameters, it is equivalent to call () in JS ();
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 );

 

VII. Anonymous Functions

Because of the variable functions, there are multiple callers (func ()/$ func ();)
Therefore, an anonymous function is generated to make function calls more identical !!!
Declare an anonymous function. The semicolon after the function body is required!

The anonymous function itself is also a variable. var_dump () is used to detect the Object type.

DEMO code:

 1 function func(){} 2    $fun = "func"; 3    $fun(); 4    func(); 5     6 $func = function(){ 7        echo "I am ZXX<br />"; 8 }; 9 $func(10);10 var_dump($func);
8 Include & require

1. Both functions are used to import external PHP files to the current file.

2. Differences between the two: When the Referenced File is incorrect, the include will generate a warning and will not affect subsequent code execution.
However, require produces a fatal error, and all subsequent code will not be executed.

3. Generally, when used to import certain files at the top of the file, use require to import the files. If the Import fails, the file will not be executed;
In some branch conditions, if you import and execute some operations, you generally use include. Once an error is reported, other branch functions are not affected.

4. include_once and require_once indicate that the file can be imported only once. If this function is called multiple times, the subsequent statement will judge whether the file is imported.
Then decide whether to import the new file.
(When checking whether a file is imported, you only need to check whether the file has been imported or not)
Include ("test.txt ");
Require_once "test.txt"; // because include is in, require does not import any more.

5. include and require can be used to import various types of files.
This is equivalent to copying a copy of the current file. However, during the copy process, the PHP engine will compile properly to ensure that no errors will occur.

6. include and require are functions and commands! PHP provides instructions for many common functions.
Eg: echo ("11"); // function syntax echo "11"; // command syntax


DEMO code:

1 $num = true;2    if($num){3        include'functionDate.php';4            //require "functionDate.php";5            func1();6        func2();7        func3();8 }9 echo "haha";

 

9 PHP Closure

In PHP, The subfunction cannot directly access the local variables of the parent function. You must use the User keyword to pass down !!!

1 $ a = 12; 2 function func1 () {3 $ num = 10; 4 $ num1 = 11; 5 $ func2 = function () use ($ num, $ num1) {// use the use keyword to pass the local variable 6 global $ a; 7 var_dump ($ a); 8 var_dump ($ num); 9 var_dump ($ num1); 10 }; 11 return $ func2; 12} 13 $ f = func1 (); 14 $ f ($ num );

 

 

 


 

 

 

Author: XI Zhao hope
Source: http://www.cnblogs.com/hope666/

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.