在 php 中使用电子邮件 ID 作为数组的键是否安全
is it safe to use a email id as a key for array in php
我正尝试在 php
中执行以下操作
array(
'email@domain.com' => 'something',
...
);
我读到 php 接受数字或有效字符串作为键。
将电子邮件 ID 作为上述数组中的键是否安全?
安全的意思是,在使用
进行编码时,这可能会导致任何类型的错误或异常或问题
注意:
我会在这个数组中有几个 100 项,在我这样做的时候有什么需要注意的吗
请告诉我
简答:如果它可以是PHP中的字符串,如今,它可以安全地用作关联数组键。
长答案: 来自PHP manual:
The key can either be an integer or a string. The value can be of any
type.
Additionally the following key casts will occur:
Strings containing valid integers will be cast to the integer type.
E.g. the key "8" will actually be stored under 8. On the other hand
"08" will not be cast, as it isn't a valid decimal integer.
Floats are
also cast to integers, which means that the fractional part will be
truncated. E.g. the key 8.7 will actually be stored under 8. Bools are
cast to integers, too, i.e. the key true will actually be stored under
1 and the key false under 0.
Null will be cast to the empty string,
i.e. the key null will actually be stored under "".
Arrays and objects
can not be used as keys. Doing so will result in a warning: Illegal
offset type.
If multiple elements in the array declaration use the
same key, only the last one will be used as all others are
overwritten.
我正尝试在 php
中执行以下操作array(
'email@domain.com' => 'something',
...
);
我读到 php 接受数字或有效字符串作为键。
将电子邮件 ID 作为上述数组中的键是否安全? 安全的意思是,在使用
进行编码时,这可能会导致任何类型的错误或异常或问题注意: 我会在这个数组中有几个 100 项,在我这样做的时候有什么需要注意的吗
请告诉我
简答:如果它可以是PHP中的字符串,如今,它可以安全地用作关联数组键。
长答案: 来自PHP manual:
The key can either be an integer or a string. The value can be of any type.
Additionally the following key casts will occur:
Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8. Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
Null will be cast to the empty string, i.e. the key null will actually be stored under "".
Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.