-4

I need some help automatically generating 250 coupon codes when i run the program.

It must:

  • create 250 coupon codes
  • the coupon codes must be 10 characters long
  • the coupon codes must begin with "IA"

This is my code:

function generateCouponCode($length = 10) {
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $ret = '';
    for($i = 0; $i < $length; ++$i) {
        'prefix'=>'IA',
        $random = str_shuffle($chars);
        $ret .= $random[250];
    }
    return $ret;
}

2 Answers2

0

This might work.

function coupon($l){

  $coupons = array();

  for($i = 0;$i < 250;$i++){
      $r=  "IA".substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',$l-2)),0,$l-2);
  array_push($coupons,$r);
  }

   //returns 250 coupons as an array
   return $coupons;

}

coupon(10);
  • Thanks for answering my question but the code you provided doesnt seem to be working. When i run the code nothing happens. Whereas in my code when i run the program the result is something like this: IAS3DJ6BW IA3JS1HSK – user3180286 Mar 06 '17 at 09:41
0
function coupon($l){

  $coupons = array();

  for($i = 0;$i < 250;$i++){
      $r=  "IA".substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',$l-2)),0,$l-2);
  array_push($coupons,$r);
  }

   //returns 250 coupons as an array
   return $coupons;

}

coupon(10);

This returns an array.

Try print_r(coupon(10)); instead of coupon(10);