2

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
  • string concatenation is with a . Commented Jul 3, 2011 at 20:07

8 Answers 8

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);
Sign up to request clarification or add additional context in comments.

Comments

1

Add them into an array:

$security_number = array(1,2,3,4,5,6,7);

1 Comment

Then use shuffle on those numbers and loop em... php.net/manual/en/function.shuffle.php
1

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?

1 Comment

$a and $b must be concatenated? :P
0

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.

Comments

0

Why not store all seven in an array, if you only want to use 1 variable that is.

Comments

0

You could use an array. All of you generated values would be in the array $array[x]

Comments

0

Why not use:

rand(1000000, 9999999)

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.