John Davidson

php - Prevent interpreting leading zero for number as octal

0 comments
Message:


JavaScript allows us to use use strict to prevent using octals with leading zero (throws an error, but better than using octal value without notifying)


How can we do the same with php? Can we set any global flag that will prevent PHP from interpreting leading zero as octal?



My Answer:
In PHP, there is no built-in feature or global flag to prevent interpreting leading zeros as octal numbers. However, you can achieve similar behavior by explicitly converting the number to decimal using the `intval()` function.

For example, instead of writing:

php
$number = 0123;


You can write:

php
$number = intval('0123');


This will ensure that the number is treated as decimal and not as an octal number.

Rate this post

5 of 5 based on 7249 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us