benp
benp

Reputation: 43

How does Casting work in PHP?

What doesn't this work:

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

But this and this does?

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

Upvotes: 3

Views: 301

Answers (6)

Shivaram Mahapatro
Shivaram Mahapatro

Reputation: 125

you are explicitly typecasting using (int)

Better use intval().

Upvotes: 0

Peter Lindqvist
Peter Lindqvist

Reputation: 10210

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

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

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

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382909

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

Upvotes: 1

CodeJoust
CodeJoust

Reputation: 3810

To use hexadecimal notation precede the number with 0x.

Therefore,

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

Upvotes: -1

SilentGhost
SilentGhost

Reputation: 320019

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

Upvotes: 11

Jerome
Jerome

Reputation: 8447

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

Upvotes: 14

Related Questions