PHP function declaration and Usage Details, php function Usage Details

Source: Internet
Author: User

PHP function declaration and Usage Details, php function Usage Details

Function

1. The function name is one of the identifiers. It can only contain letters, numbers, underscores (_), and cannot start with a number;

The name of the function must comply with the "small hump rule"FUNC(),func(),Func();

The function name is case insensitive;

The name of a function cannot be the same as that of an existing function;

2.function_exists("func");Checks 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, you need to use the return keyword to return them;

2. Global variables: variables declared outside the function are called global variables;

3. By default, local variables are used in (commonly used) functions. To use global variables in a function, use the global keyword to introduce global variables;

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

4. $ GLOBALS [''] global array;

$ GLOBALS ['a3 '] array, which is a global array built in by PHP. You can directly add values to the array, Whether Declared inside or outside the function, and 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 an internal variable address, internal variable, and external variable;

If the parameter of the function contains an address symbol, the real parameter must be a variable instead of a literal value when calling the function;

Eg: func ($ a1, $ a2) is incorrect for func ($ a1, 2 ).

Static variables]

Static variables: declare with the static keyword, static $ num = 10;

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 to share the same static variable.

[Function parameter transfer method]

In PHP, the number of parameters can only be more than the number of parameters, but cannot be less than the number of parameters. Otherwise, an error is reported.

1. Pass common parameters:

  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, modify the variable inside 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 value.

3. default parameters:

Function func ($ a, $ B = 10) {return $ a + $ B;} echo func (30); // The default value of $ B is 10.

If there are both default parameters and non-default parameters, the default parameter list must be 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 ); // obtain each parameter according to the following table.

[Variable Functions]

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 both functions is the callback function, which indicates executing the current callback;

The difference is that the second parameter call_user_func_array () is an array, and each value of the array is assigned to the callback function parameter list, which is equivalent to applying () in js. However, call_user_func, is to directly expand the callback function parameter list to 2nd-multiple parameters, equivalent to the 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 );

[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 ($ a) {echo "I am an anonymous function {$ a} <br/>" ;}; // declare the name after the anonymous function body; $ 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 ";

The above is a detailed description of function declaration and usage in PHP provided by the editor. I hope it will be helpful to you. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

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.