问题
I'm creating a php script which creates a random number which is 7 digits long and doesn't contain 0's, the only problem with this is that I have to call the function rand 7 times and store every number generated in a different variable, but I was wondering how I could put these numbers together in a variable instead of adding them together
something along the lines of
$security_number = $1&&$2&&$3 and so on.
thanks in advance.
回答1:
I think I know what you are trying to achieve. You need the mindset that you actually want a seven character string of digits. For example, if your number was 0028172, if it is stored as a number then just like a digital calculator it will be displayed as 28172 unless you use string formatting.
If you already have code creating the individual digits, simply use concatenation:
$securitycode = $digit1 . $digit2 . $digit3 . $digit4 . $digit5 . $digit6 . $digit7;
Hopefully this helps you.
回答2:
$codeLength=7;
$codeGenSeq=array_flip(array(1,2,3,4,5,6,7,8,9));
$code="";
for($i=0;$i< $codeLength;$i++)
$code .= array_rand($codeGenSeq);
回答3:
Add them into an array:
$security_number = array(1,2,3,4,5,6,7);
回答4:
I will answer in the form of another question:
If I have:
$a = 5;
$b = 7;
$c = ???;
What operation needs to be performed on $a and $b in order to give $c a value of 75?
回答5:
Why not store all seven in an array, if you only want to use 1 variable that is.
回答6:
You could use an array. All of you generated values would be in the array $array[x]
回答7:
Why not use:
rand(1000000, 9999999)
回答8:
If you concatenate the digits together with the dot (.) operator, the resulting string can be evaluated as a number automatically by php, or explicitly with intval():
$security_number = $var1 . $var2 . $var3 . $var4 . $var5 . $var6 . $var7;
or
$security_number = intval($var1 . $var2 . $var3 . $var4 . $var5 . $var6 . $var7);
来源:https://stackoverflow.com/questions/6565237/php-storing-multiple-integer-variables-in-1-variable