Home >Backend Development >PHP Problem >How to use an array to generate a verification code?
1. Several array functions that need to be used (using the code examples in 2):
a.range
(start value, end value): Generate an array within the specified range.
<?php $array=range('a','z');//生成一个从a-z的数组 ?>
b.<strong>array_merge</strong>
(): Merge one or more arrays.
Subscript conflicts are divided into two situations:
’ ’ ’ ’ s ’ s ’s ’ s ‐ The element overwrites the previous element
<?php $array1=range('a','z'); $array2=rrange(0,9); $array3=range('f','z'); $array=array_merge($array1,$array2,$array3);//将$array1,$array2,$array3合并至$array
c.
array_rand(The input array specifies how many units you want to remove): Remove one or more units from the array random cells and returns one or more keys of random entries. If only one is taken out, array_rand() returns the key name of the random unit. Otherwise, an array containing random keys is returned. Once completed, you can get the random value of the array based on the random key. If the retrieved quantity exceeds the length of array, an E_WARNING error will occur and NULL will be returned.
<?php
$index=array_rand($array,4);//从$array中取出4个单元
shuffle(Array to be operated): Shuffle (randomly arrange the order of units) an array.
<?php shuffle($index);//打乱下标
<?php
$array=range('a','z');
$array=array_merge(range('a','z'),range('A','Z'),range(0,9),range('f','z'));
$index=array_rand($array,4);
shuffle($index);
$str='';
foreach($index as $i){
$str.=$array[$i];
}
echo $str;
Recommended:
The above is the detailed content of How to use an array to generate a verification code?. For more information, please follow other related articles on the PHP Chinese website!