Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
928 views
in Technique[技术] by (71.8m points)

关于php 位运算 移位的疑惑

var_dump( (pack("C" , 1)<<1) ==pack("C" , 1) );
为什么是true呢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

牵涉到 php 比较的时候,类型转换
1、pack("C" , 1)<<1 , 结果是 int(0)
2、pack("C" , 1) , 结果是 string("")

其实就简化成了 0 == "" 的比较

PHP 在比较的时候,如果类型不相等,会进行类型转换
这里就会把 string("") 转换成 int 类型,也就是会变成 int(0),所以这里返回 true

你要严格判断,可以使用 ===

var_dump( (pack("C" , 1)<<1) ===pack("C" , 1) ); 结果是 false


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...