: This article mainly introduces the PHP code for generating red packets. For more information about PHP tutorials, see. The code is as follows:
/*** @ Param $ total [total red envelopes you want to send] * @ param int $ num [send a few] * @ return array [generate red envelope amount] */function getRedGift ($ total, $ num = 10) {$ min = 0.01; $ wamp = array (); $ returnData = array (); for ($ I = 1; $ I <$ num; + + $ I) {$ safe_total = ($ total-($ num-$ I) * $ min)/($ num-$ I ); // if ($ safe_total <0) break; $ money = @ mt_rand ($ min * 100, $ safe_total * 100)/100; // Generate a random red packet amount $ total = $ total-$ money; // total remaining red packets $ wamp [$ I] = round ($ money, 2 ); // retain two valid numbers} $ wamp [$ I] = round ($ total, 2); $ returnData ['moneysum'] = $ wamp; $ returnData ['newtotal'] = array_sum ($ wamp); return $ returnData;} // test $ data = getRedGift (100, 10); print_r ($ data ); // result: /* Array ([1] => 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) */
The code has been written here, and I hope it will help you! For more code about the php red packet generation program, visit our website for more information. thank you!
The above describes the simple code for generating a red envelope in PHP, including some content. I hope my friends who are interested in the PHP Tutorial will be helpful.