PHP closures Get the difference between external variables and global keyword declaration variables

Source: Internet
Author: User
Tags php write
Closures are a common concept that we can often use with callback functions to make the code more concise and readable. This article mainly introduces the PHP closure to get external variables and the Global keyword declaration variable differences, the need for friends can refer to the following

Recently, when learning Workerman, more frequent contact with the callback function, often in use because of the way the worker is used, the two different ways to invoke the external worker variables, Just tidy up the PHP closure. Gets the difference between the external variable and the global keyword declaration variable.

Closed Package

Closures are a common concept that we can often use with callback functions to make the code more concise and readable.

Closures can be copied in such a way that the function uses variables from the parent scope. Such as:

$global = ' Hello '; $bbb = function () use ($global) {  echo $global. \ n ";}; $BBB ();//output ' Hello '

Global keyword Declaration variable

Declaring a variable through global also allows the function body to invoke variables outside of the function, but unlike use, the Globle keyword causes a reference to be created with the same name as the external variable, and changes to the variable within the function will also affect the extraterritorial variables.

$global = ' Hello '; $fun = function () {  global $global;  $global = ' world ';  echo $global. " \ n ";}; $fun ();//Output ' world '

This simply creates a reference to the same name, and does not change the scope of the original external variable $global, which means that the call in another function still needs to be declared or used closures

$global = ' Hello '; $fun = function () {  global $global;  $global = ' world ';  Echo ' A: '. $global. " \ n ";}; $CCC = function () {  echo ' B: '. $global;}; $fun () $CCC ()/*

Output

A:worldnotice:undefined Variable:global in xxxxxxx on Line xx*/

Change the code a little bit, which makes it easier to compare the two ways of accessing external variables, such as the closure and global keyword declaration variables.

<?php$global = ' Hello '; $fun = function () {  global $global;  $global = ' world ';  Echo ' A: '. $global. " \ n ";}; $BBB = function () use ($global) {  $global = ' China ';  Echo ' C: '. $global. " \ n ";}; $fun (); Echo ' B: '. $global. " \ n "; $bbb (); Echo ' d: '. $global;

Here B and D two outputs can be seen, global changes the value of the external variable, and the closure method does not.

Output:

A:worldb:worldc:chinad:world

Finally, a more classic example of using anonymous functions, closures and callback functions in an official document:

Class cart{  Const Price_butter = 1.00;  Const Price_milk  = 3.00;  Const Price_eggs  = 6.95;  protected  $products = Array ();  Public function Add ($product, $quantity)  {    $this->products[$product] = $quantity;  }  Public Function getquantity ($product)  {    return isset ($this->products[$product])? $this->products[$ Product]:      FALSE;  }  Public Function Gettotal ($tax)  {    $total = 0.00;    $callback =      function ($quantity, $product) use ($tax, & $total)      {        $pricePerItem = constant (__class__ . "::P rice_".          Strtoupper ($product));        $total + = ($pricePerItem * $quantity) * ($tax + 1.0);      };    Array_walk ($this->products, $callback);    Return round ($total, 2);}  } $my _cart = new cart, $my _cart->add (' Butter ', 1), $my _cart->add (' Milk ', 3); $my _cart->add (' eggs ', 6);p rint $my _ Cart->gettotal (0.05). "\ n";

Summarize

The above is a small part of the introduction of the PHP closure to get external variables and the Global keyword declaration of the difference between the variables explained, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of PHP Chinese network!

Articles you may be interested in:

Using good anyproxy to improve the collection efficiency of public number articles

PHP Write public number article page collection method explained

How to capture the details of the public Number history Message page

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.