Skip to content
PHP Logo

How does PHP interpret numeric literals with a leading zero, and what potential issues can arise from this?

When PHP interprets numeric literals with a leading zero, it treats them as octal (base 8) numbers instead of decimal numbers. This can lead to unexpected results or errors in calculations if not handled properly. To avoid this issue, you can explicitly cast the numeric literal as an integer to ensure it is interpreted as a decimal number.

php
// Explicitly cast the numeric literal as an integer to interpret it as a decimal number
$number = (int) "0123";
echo $number; // Output: 123