Every e-commerce website now has one or more types of discount/coupon systems to share with you how to generate a unique promotion/discount code in PHP. A promotional code system is used to track user sources. For example, a promotional code is generated when a host is linked to another page during promotions, and more promotional codes are available. Therefore, we will discuss the implementation process of such a discount code today.
Requirements
The Code should be easy to remember, so it is a good idea to keep the length short so that users can easily remember it.
No special characters! It should be a combination of letters and numbers, because it will always be easier for users to remember
The length promotion/discount code is correct. There is no standard length, because it depends on the length you want to generate. For example, if you want to generate code 1000, you need at least 4 characters of code. The promotion/discount code is generally 4 to 8 characters in length, but it depends on your requirements.
Well, let's get started! Let's take a look at the code and I will explain it in detail. It is easy
Copy codeThe Code is as follows:
<? Php
/**
* @ Param int $ no_of_codes // defines an int type parameter to determine how many discount codes are generated.
* @ Param array $ exclude_codes_array // defines an array of the exclude_codes_array type.
* @ Param int $ code_length // defines a code_length parameter to determine the length of the discount code.
* @ Return array // returns an array.
*/
Function generate_promotion_code ($ no_of_codes, $ exclude_codes_array = '', $ code_length = 4)
{
$ Characters = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
$ Promotion_codes = array (); // This array is used to receive the generated discount code
For ($ j = 0; $ j <$ no_of_codes; $ j ++)
{
$ Code = "";
For ($ I = 0; $ I <$ code_length; $ I ++)
{
$ Code. = $ characters [mt_rand (0, strlen ($ characters)-1)];
}
// If the four-digit random number is no longer defined in the $ promotion_codes Function
If (! In_array ($ code, $ promotion_codes ))
{
If (is_array ($ exclude_codes_array ))//
{
If (! In_array ($ code, $ exclude_codes_array) // exclude used discount Codes
{
$ Promotion_codes [$ j] = $ code; assign the generated discount code to the promotion_codes array.
}
Else
{
$ J --;
}
}
Else
{
$ Promotion_codes [$ j] = $ code; // assign the discount code to the array
}
}
Else
{
$ J --;
}
}
Return $ promotion_codes;
}
Echo 'Echo '<pre> ';
Print_r (generate_promotion_code (50, '', 4 ));
Echo '</pre> ';
?>
The Code consists of three parameters,
The first parameter is the number of discount codes you want to generate (50 discount codes are generated here ). The second parameter exclude array ensures that a unique discount code is generated in the current list. If you already have unused code in the database, you can pass it to exclude. The last parameter is the length of the discount code. This function returns the discount code of the specified length. Here is the four-digit discount code.
Here I have used a combination of numbers and upper-case letters and assigned a string to $ characters. You can use a combination of lower-case letters or any other letters for trial. This function is used to generate a unique discount code. This is a PHP version. Next time, I will give it to a NET version, hoping to help you.