Reputation: 4808
PHP manual says Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +).
means integer can be decimal, hexadecimal , octal and binary.
so simple if i have echo 014;
it first converting into base 10 and output give 12
.
Why it converting automatically to base 10 , but on the other hand manual say that integer can be specified in octal also.
also look a example of type casting var_dump((int) 014)
it outputs int 12
.also this time it converting to base 10, but manual say octal is itself also a integer.
The problem is with manual language. it should say that type casting to integer means (int)
is decimal integer conversion instead of saying that it would convert it into integer. there is difference between integer and decimal integer .
Upvotes: 0
Views: 614
Reputation: 2176
> means integer can be decimal, hexadecimal , octal and binary.
No, it means an integer can be specified in decimal, octal, or binary by the programmer. You have to understand that under the covers computers only deal in binary, so the ability to express a number in decimal, hexadecimal, octal or binary is just there as a convenience for programmers. It doesn't make any difference to the computer as it only sees binary.
You type 014 => Computer sees 00001100
You type 12 => Computer sees 00001100
You type 0xc => Computer sees 00001100
The computer can't tell the different formats apart.
> Why it converting automatically to base 10, but on the other hand manual say that integer can be specified in octal also.
Because if you check the PHP documentation for echo you will see that it accepts a string parameter only. If you pass it other types it has to figure a way to implicitly cast those to a string. Relying on implicit type casting is not a good idea and you will run into problems like you are experiencing. Try to always explicitly cast. For example, if you want to echo a decimal number as hexadecimal, do this:
echo(dechex(1234));
Upvotes: 3