Reputation: 845
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.
Upvotes: 2
Views: 4099
Reputation: 101
$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);
Upvotes: 2
Reputation: 6248
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);
Upvotes: 0
Reputation: 1679
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.
Upvotes: 0
Reputation: 272657
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
?
Upvotes: 1
Reputation: 722
You could use an array. All of you generated values would be in the array $array[x]
Upvotes: 0
Reputation: 3765
Why not store all seven in an array, if you only want to use 1 variable that is.
Upvotes: 0