PHP 在 1 个变量中存储多个整数变量?
我正在创建一个 php 脚本,它创建一个 7 位长且不包含 0 的随机数,唯一的问题是我必须调用函数 rand 7 次并将生成的每个数字存储在不同的变量中,但我想知道如何将这些数字放在一个变量中,而不是将它们
按照
$security_number = $1&&$2&&$3 等行添加在一起。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
将它们添加到数组中:
Add them into an array:
我将以另一个问题的形式回答:
如果我有:
需要对
$a
和$b
执行什么操作才能给出$c< /code> 值为
75
?I will answer in the form of another question:
If I have:
What operation needs to be performed on
$a
and$b
in order to give$c
a value of75
?我想我知道你想要实现什么目标。您需要这样的心态:您实际上想要一个七个字符的数字字符串。例如,如果您的号码是 0028172,如果它存储为数字,那么就像数字计算器一样,除非您使用字符串格式,否则它将显示为 28172。
如果您已经有创建各个数字的代码,只需使用串联:
$securitycode = $digit1 。 $digit2 . $digit3 . $digit4 . $digit5 . $digit6 。 $数字7;
希望这对您有帮助。
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.
如果您只想使用 1 个变量,为什么不将所有七个变量存储在一个数组中呢?
Why not store all seven in an array, if you only want to use 1 variable that is.
您可以使用数组。所有生成的值都将位于数组 $array[x] 中
You could use an array. All of you generated values would be in the array $array[x]
为什么不使用:
Why not use:
如果您使用点 (.) 运算符将数字连接在一起,则生成的字符串可以由 php 自动计算为数字,或者使用 intval() 显式计算:
或
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():
or