This article introduces the content of PHP built-in functions using the compact (), has a certain reference value, now share to everyone, the need for friends can refer to
Today, when using tpshop, we found a PHP built-in function called
Compact (), which creates an array of variables with the arguments. If an array exists in the parameter, the value of the variable in the array is also fetched.
This can reduce the amount of point code. Like what
/** * Get shopping cart price details * @param $cartList | shopping cart list * @return Array * /public function Getcartpriceinfo ($ Cartlist = null) { $total _fee = $goods _fee = $goods _num = 0;//Initialize data. Total merchandise/savings/Commodity total if ($cartList) { foreach ($cartList as $cartKey = = $cartItem) { $total _fee + = $cartItem [ ' Goods_fee ']; $goods _fee + = $cartItem [' Cut_fee ']; $goods _num + = $cartItem [' Goods_num '];} } $result = Array ( ' total_fee ' = = $total _fee, ' goods_fee ' = $goods _fee, ' goods_num ' = $goods _ Num, ); return $result; }
In this way, the code is much simpler.
/** * Get shopping cart price details * @param $cartList | Shopping Cart list * @return Array */ Public Function getcartpriceinfo ($cartList = null) {$total _fee = $goods _fee = $goods _num = 0;//initializes the data. Total Merchandise/savings/Total number of items if ($cartList) {foreach ($cartList as $cartKey + $cartItem) {$total _ Fee + = $cartItem [' Goods_fee ']; $goods _fee + = $cartItem [' Cut_fee ']; $goods _num + = $cartItem [' Goods_num ']; }} return Compact (' Total_fee ', ' goods_fee ', ' goods_num '); }