Function declaration and use in PHP, and PHP function declaration

Source: Internet
Author: User

Function declaration and use in PHP, and PHP function declaration
Function 1. A function name is one of the identifiers. It can only contain letters, numbers, and underscores. It cannot start with a number. The name must comply with the "small hump rule" FUNC (), func (), func (); the function name is case-insensitive; the function name cannot be the same as an existing function, and cannot be the same as the built-in function name; 2. function_exists ("func"); used to check whether the function has been declared. Note that the input function name must be in string format and the returned result is true/false. When echo is printed, true is 1, false is not displayed; [Scope of variables in php] 1. local variables: variables declared inside the function, called Local variables, are used only within the function. To be used outside the function, the return keyword must be used in the function. 2. global variables: variables declared outside the function are called global variables. 3. (more commonly used) local variables are used by default in a function. To use global variables in a function, you must use the global keyword to introduce global variables. The variable name in the function, if it is the same as the global variable name Is the local variable of the function, and global is the global variable of the function; 4. $ GLOBALS [''] global array; $ GLOBALS ['a3 '] array, which is a built-in Global Array provided by PHP. You can directly add values to the array, Whether Declared inside or outside the function, can be directly used anywhere; eg: $ GLOBALS ['a3 '] = 10; 5. you can use global variables in a function by passing parameters. However, after passing the parameters, you can use global variables in the function, the outside will not change unless the passed parameter is the address. function func ($ a1, & $ a2) {} func ($ a1, $ a2); (cause) $ a1 is a local variable, internal variable, external variable, $ a2 is also the internal variable address, which changes internally and externally. If the function parameters have the address symbol, the real parameter must be a variable when calling the function, it cannot be a literal; for example: func ($ a1, $ a2) to func ($ a1, 2), the [static variable] static variable: Use st Atic keyword declaration, static $ num = 10; static variables features: 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 to share the same static variable. [Function parameter transfer method] the number of parameters in PHP can only be more than the number of parameters, but cannot be less than the number of parameters. Otherwise, an error 1 is reported. regular parameter passing: function fun ($ a) {$ a + = 10; return $ a;} echo fun (10); 2. parameters of the reference type: $ a = 10; function func (& $ a) {$ a + = 10;} func ($ B); reference parameter transfer, variable modification within the function, synchronous changes outside the function. The parameter is a reference parameter. The real parameter can only be a variable, not a literal value. 3. default parameter: function func ($ a, $ B = 10) {return $ a + $ B;} echo func (30 ); // $ B's default parameter is 10. If both default parameters and non-default parameters exist, the default parameter list must be placed behind the non-default parameter list, that is, the order of values of non-default parameters must be ensured. func_get_args (); // obtain the list of all parameters (array) func_num_args (); // obtain the total number of all parameters, equivalent to count (func_num_args (); func_get_arg (0 ); // according to the following table, take each parameter [Variable Function] to 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 (); [callback function] 1. use variable functions to customize callback functions; function ($ 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 the two functions is the callback function, indicating that the current callback is executed. The difference is that the second parameter call_user_func_array () is an array, and assign each value of the array to the callback function parameter list, which is equivalent to apply () in js. While call_user_func is the list of callback function parameters, directly expand to 2nd-multiple parameters, which is equivalent to call () in js; eg: call_user_func_array ("func", array (, 3); ---> func, 3); call_user_func ("func" 1, 2, 3); ----> func (1, 2, 3 );[Anonymous functions]Variable Functions are called in a variety of calling methods, $ fun ()/func (). Therefore, in order to make function calls more unified, an anonymous function is generated.
Declare the name after the anonymous function body !!!
The anonymous function itself is also a variable. var_dump is used to detect the object type;

Common functions:
Function func () {$ fun = "func"} $ fun (); // func ();
Anonymous functions:
$ Func = function ($ ){
Echo "I am an anonymous function {$ a} <br/> ";
}; // Declare the name after the anonymous function body; required
$ Func (10 );
Var_dump ($ func );

Example: Calculate the class of a number:

Function jiec ($ num) {static $ jie = 1; // if ($ num> 0) {$ jie * = $ num; // 3 jiec (-- $ num);} return $ jie;} echo jiec (10 );

 
[Recursive functions]

It refers to calling the function's own operations within the function. When the outer function body encounters its own function call, it continues to enter the inner function execution, and the second half of its function is not executed yet, after you know that the innermost function is executed, it will be executed gradually;
Function func ($ num) {echo $ num. "<br/>"; if ($ num> 0) {func ($ num-1); // func (-- $ num); try different results! // Func ($ num --);} echo $ num. "<br/>";} func (10 );

[Include/require]
1. The two are used to introduce external php files to the current file: include 'a. php'; include ('A. php ');
2. the difference between the two: (handle errors differently) when a file error is introduced, include will generate a warning and will not affect subsequent code execution, while require will generate an error and subsequent code will not be executed;
3. When it is used to import some files at the top of the file, use require to import the files. If it fails, the file will not be executed;
If you import and execute some operations in some Branch Conditions, once an error is reported, the execution result is not affected.
4. include_once and require_once indicate that the file can be imported only once. If the function is called multiple times, the subsequent file will judge whether the file is imported and 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 and not how to import it .)
5. include/require can be used to import various types of files, which is equivalent to copying a copy of the current file. However, during the copy process, the PHP engine will compile the file properly to ensure that no errors will occur.
6. include and require are functions and commands! PHP provides execution methods for many common functions. For example, echo ("111") is used for function writing and echo "111" is used for instruction writing ";

If you think it's not bad, move your finger and give it a compliment! You will be shipping !!! 

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.