douyaju4749 2016-04-28 12:23
浏览 11
已采纳

使用电子邮件ID作为php中的数组键是否安全[重复]

This question already has an answer here:

I am trying to do the below in php

array(
  'email@domain.com' => 'something',
  ...
);

I did read that php accepts and number or valid string as key.

is it safe to have email id as key in the array as above ? by safe I mean is possible that this could cause any kind of error or exception or problem when coding wit this

Note: I would be having several 100 items in this array is there something that I need to take care while I do this

Please let me know

</div>
  • 写回答

1条回答 默认 最新

  • douti0687 2016-04-28 12:36
    关注

    Short answer: If it can be a string in PHP, nowadays, it can safely be used as an associate array key.

    Long answer: From the 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部