In php, is 123==0123?

PHPServer Side ProgrammingProgramming

The answer is No. This is because 0123 means 123 with base 8 (an octal number) and its equivalent in decimal is 83.

Prefixing a number with 0 indicates that it is an octal (base 8) number. This is similar to the fact that 0x indicates hex (base 16) numbers.

Consider the below lines of code −

Example

Live Demo

var_dump(123);
var_dump(0123);

Output

This will produce the following output −

int 123
int 83

This is due to the fact that 0123 is an octal notation (notice the 0 at the beginning), whereas 123 is a decimal number.

Now consider the below code −

Example

var_dump(79);
var_dump(079);

Output

This will produce the following output −

int 79
int 7
Published on 06-Apr-2020 09:12:30