Nabi K.A.Z.
Nabi K.A.Z.

Reputation: 10714

Comparison Operators

Reffer this link.

I know operand with string type translate to number, and then usual math

But see these sample codes:

echo intval(1e1);       // 10
var_dump("1e1" == 10);  // true, and it's ok

echo intval(0x1A);      // 26
var_dump("0x1A" == 26); // true, and it's ok

echo intval(042);       // 34
var_dump("042" == 34);  // fasle, Why ?!!!

Why last code return false.

Upvotes: 3

Views: 457

Answers (3)

raina77ow
raina77ow

Reputation: 106385

That's because string-to-number conversion in PHP is based on some ancient C function - strtod. And its rules are as follows:

The expected form of the (initial portion of the) string is optional leading white space as recognized by isspace(3), an optional plus ('+') or minus sign ('-') and then either (i) a decimal number, or (ii) a hexadecimal number [...]

A decimal number consists of a nonempty sequence of decimal digits possibly containing a radix character (decimal point, locale-dependent, usually '.'), optionally followed by a decimal exponent. A decimal exponent consists of an 'E' or 'e', followed by an optional plus or minus sign, followed by a nonempty sequence of decimal digits, and indicates multiplication by a power of 10. [... ]

A hexadecimal number consists of a "0x" or "0X" followed by a nonempty sequence of hexadecimal digits possibly containing a radix character, optionally followed by a binary exponent. [...]

As you see, '1e1' string has non-empty sequence '1' followed by a decimal exponent 'e1'. So, it will be converted into a decimal number - and becomes 10.

'0x1A' string follows the rules for hexadecimal number, and will be converted into 26 accordingly. But as there's no specific rule for octadecimal number, '042' will be converted into a plain decimal - and becomes 42. Which is, of course, not equal to 34.

This should not be confused with how number literals are parsed by PHP itself. A number literal that starts with 0 is considered representing an octadecimal. So, intval(042) is essentially the same as intval(34) - but not the same as intval("042").

Upvotes: 9

Deepak
Deepak

Reputation: 6802

That's how PHP rolls. It is because when you specify a string and convert, it gets converted to a number, In your case, the first 1e1 means 1 exponent 1, the 0x1A is hexadecimal representation and final part 042 itself is a number and it is converted to 42 but intval(042) means octal representation of integer 34.

Upvotes: 3

Kris
Kris

Reputation: 6112

I don't know all the math mumbo jumbo but,

Because the first two examples are clear what you are trying to do.. convert it to an exponent, and convert it to hex. But the third solution is not clear on what you are doing, so it converts it to 42. Think about it the other way.. You are trying to solvve a problem and for some reason "042" does not equal 42. you would be very confused

So use what you need, if you need an int representation of 042 then cast to an int, but if it is a string don't expect it to work that way.

Upvotes: 1

Related Questions