Getting Started with PHP Basics (ii) "PHP function Basics"

Source: Internet
Author: User
Tags getting started with php php basics

Getting Started with PHP Basics (ii.)--Function basicsUnderstandafter a quick introduction to PHP Basics (i),let's share the basic function of PHP.

This part is mainly about: declaration and use of functions, scope of variables in PHP, static variables, parameter passing of functions, variable functions, callback functions, anonymous functions, Include&require, PHP closures

PHP's function base ↓↓↓

I. declaration and use of functions

1. Identifiers: variable names, attribute names, method names, function names, class names, etc. in the program are collectively referred to as identifiers;
naming requirements for ① identifiers:
as long as it is an identifier, the name only consists of letters, numbers, underscores, and cannot begin with numbers;
naming conventions for ② identifiers:
class name: To conform to the "Big Hump Law", the first letter of each word all uppercase! personname
Other identifiers: conforms to the "small hump law". The first letter is lowercase, and then the first letter of each word is capitalized. Mynameishang
or use the Hungarian "dental nomenclature", where all the letters are lowercase and the words are separated by underscores. my_name_is

2, the function name requirements:
The ① function name is also one of the identifiers. Conforms to the requirements and specifications of identifiers.
② function names are not case-sensitive.
The ③ function name cannot be duplicated with the existing function name and cannot be duplicated with the built-in function name.
the ④bool type function_exists ("func") is used to detect the existence of a function name.

Second, the scope of variables in PHP

1, local variables: variables declared inside the function, called local variables, can only be used inside the function, if you need to use outside the function, you need the return keyword to return the variable.
2, global variables: declared outside the function is called a global variable.
3. Note : In PHP, the global scope cannot access local variables, and local scopes cannot access global variables.
If you want to use global variables in a function, you need to introduce global variables into them using the global keyword.
If a variable has the same name as a global variable in a function, the global statement uses a local variable;
4, globals[] Global array
globals[] Array, which is a global array built into PHP. You can add keys and values directly to an array, and can be used anywhere, regardless of the Wai declaration within the function.
eg:globals[' name ']= "Hanghao";   
that is, wherever you declare, you can use globals[' name ' Anywhere
5, the function requires the use of global variables, there is also a way to "function parameter"
Global variables can be passed through the parameters of the function. But the parameter passed in is the formal parameter of the function, that is, the local variable inside the function, the variable and the global variable are actually two variables, the internal change, the global variable will not change.
If you need to change a local variable inside a function, you can declare the formal parameter as the address
function func ($a 1, $a 2) {}   
That is, in the function modifies the A1, the global A1 does not change, the function modifies the A2, the global A2 also changes.
Note : If a function declares a formal parameter that is an address, the call must pass in the variable, not the literal.
For example, the function call above: Func ($a 1, $a 2); √func ($a); x
because the literal cannot be in the variable, the address cannot be taken.
6. In PHP, a child function cannot use a variable in the parent function
if the child function is to use a variable of the parent function, the child function can be used as a variable function, with the USE keyword passing the variable of the parent function into the child function.

function  func () {$a=10;  $func=func () use ($a) {echo $a; }}

The Use keyword can only be used with variable functions, not in the form of function func () {}.

third, static variables

1. Use the variable declared by the Scatic keyword to become a static variable. Scatic $a = 10;
2, the characteristics of static variables:
① static variables are declared when the function is loaded.
② static variables are not freed after the function call is complete.
③ static variables are declared only once during the entire execution of the script, and the variables are released after the execution of the script is complete.
④ the same function, calls multiple times, and shares the same static variable.

iv. parameter passing of functions

1, in PHP if there are parameters to pass: The argument can only be more than the formal parameters, can not be less than the formal parameters!!
2. General parameter pass: function func ($a) {} func (123);
3. Reference parameter passing: function func (& $a) {} func ($b);
① The address of the argument is passed through &, so the parameters are changed inside the function, and the external arguments are changed.
② If the formal parameter is a reference parameter, the argument must be a variable, not a literal. Because the literal no address!! Func (10); X
4. Default parameters: function func ($b, $a =10) {} func;
is $ can not pass, the default is ten; $b must be passed, otherwise the error is undefined when used.
Note : If you have default parameters in the parameters, you need to put all the non-default parameters in front of them, and all the default parameters will be followed.   

5, variable parameter list: Because PHP arguments can be more than formal parameters, then we can pass n arguments, and through the PHP built-in function to take the corresponding parameters.
Var_dump (Func_get_args ()); Returns the array format and takes the list of all arguments;
Var_dump (Func_num_args ()); Returns the total number of argument lists; count (Func_num_args ());
var_dump (func_get_arg (0)); Incoming index, take the first few parameters Func_get_args () [0];

v. variable functions

1. Assign a function name to a variable after it is converted to a string. This variable, which is what we call the variable function, can add () to the function's contents.

function func () {}

$a = "func ()"; $a is the variable function

$a ();

Vi. Custom callback functions

1, use variable function, custom callback function
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 the two function is a callback function that represents the execution of the current callback.
The difference is that: Call_user_func_array () The second parameter is an array and assigns each value of the arrays to the parameter list of the callback function, which is equivalent to the Apply () in JS;
And Call_user_func, is the parameter list of the callback function, directly expand to write to the 2~ multiple parameters, equivalent to the call () in JS;

Call_user_func_array ("func", Array (+/-));    Func (N/a); Call_user_func ("func", "A.");    Func (n/a);

vii. anonymous functions

because of the variable function, there are two ways of calling, (func () $func ();)
There is no guarantee that the calling method is uniform, so the semicolon of the variable must not be omitted.

$func 1 = function () {};

The anonymous function itself is also a variable, with Var_dump (), and is detected as an object type

1 function func () {} 2    $fun = "Func"; 3    $fun (); 4    func (); 5     6 $func = function () {7        echo "I am ZXX <br/> "; 8}; 9 $func, Var_dump ($func);
Eight, include&require

1, the role of both: is used to introduce external PHP files into the current file.

2, the difference between the two: when the introduction of external file error, include will produce a waring level of warning, does not affect the subsequent execution of the code, and require will produce error level errors, will block the execution of the Code

3, generally, when used to import some files at the top of the file, use require import, if the import fails, then no longer execute the file;
If, in some branch conditions, the import performs some action, the include is generally used. Once an error is not affected, no other branch functions.

4, Include_once and Require_once said: The file can only be imported once, if you call this function more than once, then the statement will determine whether the file is imported.
Then decide whether to import the new file.

5. Include and require can import various types of files.
It is equivalent to copy a copy of the current file, but during the copy process, the PHP engine compiles properly to ensure that there are no errors.

6, include and require are functions, also instructions! PHP provides instructions for many common functions.
  Eg:echo ("11");//function notation echo "11";//instruction notation

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

Nine, php closure

In PHP, a child function cannot directly access the local variables of the parent function and must be passed down with the user keyword!!!

1 $a = 12; 2 function Func1 () {3    $num = 4    $num 1 = one; 5    $func 2 = function () use ($num, $num 1) {//Pass the parent function local variable with the Using keyword 6< C14/>global $a; 7        var_dump ($a), 8        var_dump ($num), 9        var_dump ($num 1),    };11    return $func 2;12}13 $f = func1 ( ); $f ($num);

If the parameters have both default parameters and non-default parameters, then the default parameter list
It must be followed by a non-default parameter list, that is, a non-default list must be assigned precedence when called.

Getting Started with PHP Basics (ii) "PHP function Basics"

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.