Req #64320 [NEW]: Proposal: deprecate the "leading 0 means octal" behaviour
From: php at richardneill dot org
Operating system: All
PHP version: 5.4.12
Package: Math related
Bug Type: Feature/Change Request
Bug description:Proposal: deprecate the "leading 0 means octal" behaviour
Description:
------------
PHP assumes that a string with a leading zero should be interpreted as
octal.
In my experience, this is almost never useful, desirable, or intentional,
however, it is a frequent trap for the beginner (or more experienced
programmer), especially when parsing user-submitted data.
The only reason for this behaviour seems to be historical, and
compatibility with strtol(). When non-programmer humans write numbers with
leading zeros, they don't usually mean base 8.
My proposal is:
1. Introduce a new string format, 0o#### (letter o), to explicitly mean
"this is an octal number". This is similar to the recent introduction of
0b#### for binary numbers.
[This part should be simple to do, and would also make the intentional use
of octal notation explicit, increasing code-readability]
2. Add an option to raise E_NOTICE any time a number is implicitly
interpreted as octal (for example "$x = 0100").
3. In a few years time, (PHP 7?), it may finally be possible to change the
default behaviour.
Test script:
---------------
Here's an illustration of a naive program that has data-dependent bugs that
are really hard to track down.
$mass_kg = "0.314" //user-submitted data, known to begin "0."
$mass_g = substr ($mass_kg, 2);
Yes, this is a silly thing to write. But it's a nasty trap for the unwary.
The above example does what is expected, and might pass many tests. But if
the user subsequently enters $mass_kg = "0.031", this will turn into 25
grams, not 31 grams. As a result, we have created a very subtle and hard to
find bug.
--
Edit bug report at https://bugs.php.net/bug.php?id=64320&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=64320&r=trysnapshot54
Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=64320&r=trysnapshot53
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=64320&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=64320&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=64320&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=64320&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=64320&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=64320&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=64320&r=support
Expected behavior: https://bugs.php.net/fix.php?id=64320&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=64320&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=64320&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=64320&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64320&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=64320&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=64320&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=64320&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=64320&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=64320&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=64320&r=mysqlcfg
Thread (3 messages)
- php at richardneill dot org