In-depth explanation of php functions and variable scopes

Source: Internet
Author: User
In-depth explanation of php functions and variable scopes

  1. Function & func (){
  2. Return "";
  3. }

2. add a reference symbol before the function during the call.

  1. $ Return = & func ();

PHP has only two types of scopes: external functions and global scopes. 2. local scope within the function.

Rule: The scope does not overlap. (Local access is not allowed globally, and local access is not allowed globally) but js can overlap.

Special: pre-defined variables (9 super-global array variables) -- note that they are array variables. Ultra Global: Super Global: it can be used globally or locally. All scopes take effect.

  1. $ _ GET ['v1 '] = "123 ";
  2. Echo $ _ GET ['v1 ']

$ GLOBALS is a super global variable that stores Super global variables. Note that there is no underline.

Modifying global variables affects the values of elements in GLOBALS;

Cause: A data space used. Equivalent:

  1. $ GLOBALS ['V8 '] = & $ v8;

2. create_function -- create a function in another way

  1. $ Func = create_function ('$ P', "echo $ p );

Var_dump ($ func); returns a function name. The name of the function created by the sub-function is lambda. In addition, there is an invisible character before the function name. Invisible characters with Ascii 0. It can also be called. $ Result = "\ x0". "lamba8" (20). you must know the function name before calling it.

The most common part appears in the callback function. Therefore, the functions created by create_function are no different from normal functions (there are three parts of the function );

However, this function cannot be called before it is specified. Therefore, you only need to execute create_function to obtain this function. Most commonly used: On the callback parameter, the create_function parameter is equivalent to a callback structure input parameter.

Anonymous function: a function created by an anonymous function has no name, but an object: the difference with create_function:

What is an anonymous function? 1. you can view anonymous functions on duty (object type in php). 2. Php implements anonymous functions through the closure class. Every anonymous function is an object of the Closure class. Closure indicates a Closure. So sometimes php calls an anonymous function a closure function. 3. Why can an object be called as a function? (Oop)

You can use the syntax of variables in the outer scope: Note: the difference between the outer layer and the Global layer. The outer layer may be global or local. The current anonymous function is defined.

The Use syntax defaults to value transfer, which can be referenced and passed. Callback1, Function name 2, Create_function3, Function anonymous Function

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.