PHP built-in functions using compact ()

Source: Internet
Author: User
Tags compact
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 '); }
Related Article

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.