开发者

How does Casting work in PHP?

开发者 https://www.devze.com 2022-12-13 05:50 出处:网络
What doesn\'t this work: (int)08 == (int)09==0 But this and开发者_JS百科 this does? (int)07==7 (int)06==6

What doesn't this work:

(int)08 == (int)09==0

But this and开发者_JS百科 this does?

(int)07==7 
(int)06==6 


08 is in octal base (because it starts with a 0), hence it is invalid. See the documentation.


because 08 and 09 are not valid octal numbers. see warning in docs.


You're type casting an invalid number in octal base.


// Syntax error
//(int)08 == (int)09==0

// This works
(int)08==0;
(int)09==0;

// This also works
(int)08 == ((int)09==0);


you are explicitly typecasting using (int)

Better use intval().


To use hexadecimal notation precede the number with 0x.

Therefore,

 $num = (int)0x9
 $num == 9
0

精彩评论

暂无评论...
验证码 换一张
取 消