PHP generates WeChat red envelope code simple

Source: Internet
Author: User
No more nonsense, all the explanations are in the code, as shown in the code below:

/*** @param $total [Amount of red envelopes you want to send]* @param int $num [send several]* @return array[generate red Envelope amount]*/function getredgift ($total, $num = ten) {$min = 0.01; $wamp = Array (); $returnData = Array (); for ($i = 1; $i < $num; + + $i) {$safe _total = ($total-($num-$i) * $min) /($num-$i);  Maximum amount of red envelopes if ($safe _total < 0) break; $money = @mt_rand ($min *, $safe _total * 100)/100;//randomly generates a red envelope amount $total = $total -$money;//Total bonus $wamp[$i] = round ($money, 2);//reserved two valid digits} $wamp [$i] = Round ($total, 2); $returnData [' moneysum '] = $wamp; $ returndata[' newtotal ' = Array_sum ($wamp); return $returnData;} Test $data = Getredgift (1),;p Rint_r ($data);//result:/*array ([] = 8.7[2] [10.09[3] = 6.23[4] = 6.87[ 5] [= 0.47[6] = 3.12[7] = 7.52[8] = 12.21[9] = 20.53[10] = 24.26) */

Code to this to everyone has finished, hope to help! More about PHP generated red Envelopes program code in the site, please visit our website to learn more information, thank you!

The above describes the PHP generated red envelope code is simple, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.