Search code examples
phptypestype-conversionintegervar-dump

Why the integer data types hexadecimal, octal and binary are getting converted to decimal type and then displayed in a browser?


I'm using PHP 7.1.11

I've following PHP code :

<?php
$a = 0123; // an octal number (equivalent to 83 decimal)
$b = 0x1A; // a hexadecimal number (equivalent to 26 decimal)
$c = 0b11111111; // a binary number (equivalent to 255 decimal)

var_dump($a); //Output : int(83) int(26) int(255) 
var_dump($b); //Output : int(26) int(255)
var_dump($c); //Output : int(255)
?>

My question is why the data contained in integer data types of octal, hexadecimal and binary are getting converted into decimal and then displayed in browser along with the new datatype integer?

Why it's not showing the original values of variables and also not showing their original data type?

Why there is a conversion step and who does this for what reason?

If we take other data types life boolean, float, string nothing like this happen. Everything works as expected then why this weird thing is happening with these integer data types?


Solution

  • There's only one kind of (integer) number, and the format of the number is completely unimportant. The values 052, 42 and 0x2A all represent exactly the same value. PHP doesn't remember how a number was created; it doesn't store that information because it's useless information that would only waste space being stored. Binary, octal and hexadecimal notations are mere convenience affordances to allow you to express numbers in the most suited notation for the context; it's all being parsed to an int of the same value and then output in decimal notation by default.