An in-depth explanation of PHP functions and variable scopes

WBOY
Release: 2016-07-25 08:59:08
Original
857 people have browsed it
  1. function &func(){
  2. return "";
  3. }
Copy code

2. When calling, add a reference symbol before the function.

  1. $return = &func();
Copy code

There are only two scopes in PHP 1. Outside the function, global scope. 2. Within the function, local scope.

Rule: scopes do not overlap. (The global cannot access the local, and the local cannot access the global) But js is overlapping.

Special: Predefined variables (9 super global array variables)--note that they are array variables. Super global: Super global: can be used both globally and locally. All scopes are valid.

  1. $_GET['v1'] = "123";
  2. echo $_GET['v1']
Copy code

$GLOBALS Super global variables are specially used to save super global variables. Note there are no underscores.

Modifying global variables will affect the values ​​of elements within GLOBALS;

Reason: A data space used. Equivalent to:

  1. $GLOBALS[‘v8’] = &$v8;
Copy code

2. create_function -- another way to create a function

  1. $func = create_function('$p',"echo $p);
Copy code

var_dump($func); Returns a function name. The function name created by this function is lambda style. And there is an invisible character before the function name. Invisible characters with Ascii 0. Can also be called. $result = "x0"."lamba8"(20); Note that you need to know the function name before calling it.

The most commonly used place appears within the callback function. Therefore, the function created by create_function is no different from an ordinary function (there are three parts of the function);

But this function cannot be called before setting, so you can only get the function by executing create_function. Most commonly used places: On the callback parameter, the parameter create_function Equivalent to a callback structure input parameter.

Anonymous function: The function created by anonymous function has no name, but an object: Difference from create_function:

What exactly is an anonymous function? 1. Treat anonymous functions as values ​​(object type in PHP) 2. Php implements anonymous function functions through the closure class. Each anonymous function is an object of the Closure class. The word Closure means closure. Therefore, sometimes PHP also calls anonymous functions closure functions. 3. Why can an object be called as a function? (oop)

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

Use syntax defaults to passing by value and can be passed by reference. Callback 1. Function name 2. Create_function 3. Function anonymous function



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!