0

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 .

2
  • And what is your question? SO isn't the place to report bugs in PHP. Not that I believe this is a bug, but my knowledge of numbers only goes so far. Still if you think you've encountered a bug in PHP, you should report it on bugs.php.net Or even head over to the PHP chatroom here on SO and discuss the matter there. But don't create a question out of it while there's no question at all. Perhaps you could edit your question and explain your findings asking here if you misunderstood it and someone can explain it to you. Commented Nov 20, 2017 at 6:24
  • Probably correct as you can input any kind of int but php will always return (or display) decimal int format (most likely because it's human readable). It's unclear however, yet obvious we would see the decimal equivalent. There is no difference in value between int and decimal int, only the way it is displayed. You can still use bitmask (binary) operations on a decimal, octal, or hexidecimal formatted integer. If you want non-human readable formats you must convert. My point is that int is int, you only see decimal integer without explicit conversion (usually to a string) Commented Dec 17, 2023 at 22:47

1 Answer 1

3

> 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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.