Aiden Ryan
Aiden Ryan

Reputation: 845

PHP storing multiple integer variables in 1 variable?

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

Answers (8)

Shyju O Sivanandan
Shyju O Sivanandan

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

bmb
bmb

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

Tim S.
Tim S.

Reputation: 56556

Why not use:

rand(1000000, 9999999)

Upvotes: 0

fabspro
fabspro

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

Oliver Charlesworth
Oliver Charlesworth

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

TheITGuy
TheITGuy

Reputation: 722

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

Upvotes: 0

Jack Franklin
Jack Franklin

Reputation: 3765

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

Upvotes: 0

Niklas
Niklas

Reputation: 30002

Add them into an array:

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

Upvotes: 1

Related Questions