: This article mainly introduces the use of functionuse for PHP closures. For more information about PHP tutorials, see.
//Counter
Function counter (){
$ Int = 1;
Return function () use (& $ int) {echo $ int ++ ;};
}
$ Count1 = counter ();
$ Count2 = counter ();
$ Count1 (); // output 1
$ Count2 (); // output 1
$ Count1 (); // output 2
$ Count2 (); // output 2
// Calculate the amount and tax amount of the shopping cart.
Function calculate ($ cart = array ()){
$ Amt = 0;
$ Amt_tax = 0;
$ Amt_price = 0;
$ TaxRate = 0.17;
$ Callback = function ($ goods_info, $ goods_name) use (& $ amt, & $ amt_tax, & $ amt_price, $ taxRate ){
Extract ($ goods_info );
Echo "product:". $ goods_name;
Echo "quantity:". $ num;
$ Goods_amt_tax = bcmul ($ price, $ num, 2 );
$ Goods_price = round (bcp ($ price, bcadd ($ taxRate, 1, 2), 12), 6 );
Echo "unit price:". $ goods_price;
$ Goods_amt = round (bcmul ($ goods_price, $ num, 6), 2 );
Echo "amount:". $ goods_amt;
$ Goods_tax = bcsub ($ goods_amt_tax, $ goods_amt, 2 );
Echo "tax amount:". $ goods_tax. "\ n ";
$ Amt = bcadd ($ amt, $ goods_amt_tax, 2 );
$ Amt_tax = bcadd ($ amt_tax, $ goods_tax, 2 );
$ Amt_price = bcadd ($ amt_price, $ goods_amt, 2 );
};
Array_walk ($ cart, $ callback );
Echo "total amount:". $ amt_price. "\ n ";
Echo "total tax amount:". $ amt_tax. "\ n ";
Echo "Golden tax total:". $ amt. "\ n ";
}
$ Cart = array ("apple" => array ("price" => 20, "num" => 1 ), "orange" => array ("price" => 15, "num" => 2 ));
Calculate ($ cart );
The above describes how to use function use in PHP closures, including counter content, and hope to help friends who are interested in PHP tutorials.