Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Why is the answer to this addition 20 and not 22?

Tags:

php

Why is the output to this 20, and not 22? Seeing as you're adding 10 + 0xA(which is 10 in HEX) + 2.

$a = 010;
$b = 0xA;
$c = 2;

print $a + $b + $c;

Output: 20.
like image 594
Stephen Fox Avatar asked Nov 29 '22 01:11

Stephen Fox


1 Answers

It's correct!

(Because the first number is octal so if you want it to be interpreted as a decimal you have to remove the first 0) Se:

$a = 010;  //Octal -> 8
$b = 0xA;  //Hex   -> 10
$c = 2;    //Dec   -> 2

print $a + $b + $c;  //20

Output:

20
like image 134
Rizier123 Avatar answered Nov 30 '22 14:11

Rizier123