问题描述
如果我们的IP地址如下:
If we have an ip address as below:
127.0.0.1
这两个功能是否将IP地址转换为相同的数字,或者它们是否不同且结果不同?
Does both functions convert the ip address to the same number, or do they differ and have different result?
推荐答案
它们几乎完全相同. ip2long 有时返回负值,因为PHP使用带符号的数字进行评估,而MySQL使用无符号的.
They are almost exactly the same. ip2long sometimes returns a negative value because PHP uses signed numbers for valuation, while MySQL uses unsigned.
两者都被评估为x*(2^24) + y*(2^16) + z*(2^8) + w*(2^0)
,但是在PHP中,由于长期被签署,因此对于某些IP地址将显示负值.
Both are evaluated as x*(2^24) + y*(2^16) + z*(2^8) + w*(2^0)
, but in PHP, due to the long being signed, will show negative values for certain IP addresses.
For signed long, the range is
(2^31) - 1 = −2,147,483,648 to +2,147,483,647
因此,地址转换为+2,147,483,647时会回绕并给出负值.
So, addresses while translate to over +2,147,483,647 will wrap around and give negative values.
ip2long("254.254.254.254"); // -16843010
此链接对此进行了详细说明.
This link describes this in detail.
这篇关于PHP中的ip2long()是否等于MySQL中的INET_ATON()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!