Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
In php, is 123==0123?
The answer is No. This is because 0123 represents an octal number (base 8) with a decimal equivalent of 83, while 123 is a standard decimal number.
In PHP, prefixing a number with 0 indicates that it is an octal (base 8) number. This is similar to how 0x indicates hexadecimal (base 16) numbers.
Basic Comparison
Let's examine how PHP interprets these numbers ?
<?php var_dump(123); var_dump(0123); ?>
int(123) int(83)
The first number 123 is interpreted as a decimal number, while 0123 is treated as octal due to the leading zero.
Equality Check
Here's a direct comparison to verify they are not equal ?
<?php echo "123 == 0123: "; var_dump(123 == 0123); echo "123 value: " . 123 . "<br>"; echo "0123 value: " . 0123 . "<br>"; ?>
123 == 0123: bool(false) 123 value: 123 0123 value: 83
Invalid Octal Example
Numbers with digits 8 or 9 cannot be valid octals, so PHP treats them differently ?
<?php var_dump(79); var_dump(079); // Invalid octal, only digits 7 remain ?>
int(79) int(7)
Since 9 is not a valid octal digit (octals use 0-7), PHP ignores the 9 and interprets 079 as 07, which equals 7 in decimal.
Conclusion
123 and 0123 are not equal because the leading zero makes 0123 an octal number with decimal value 83. Always be careful with leading zeros in numeric literals to avoid unexpected results.
