php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #38888 gmp_init treats leading zero as octal
Submitted: 2006-09-19 23:57 UTC Modified: 2006-10-09 23:32 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: php at richardneill dot org Assigned: pajoye (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2006-09-19 23:57 UTC] php at richardneill dot org
Description:
------------
The documentation for gmp_init implies that a number will only be parsed as hexadecimal if it begins with 0x, and decimal otherwise.

Thus:  "10"     means ten
       "0x10"   means sixteen
       "010"    means ten.

However, in practice, "010" is parsed as eight!.

Not sure whether this is a documentation bug, or an actual bug though. Personally, I'd prefer "010" to mean ten, but I am aware of some conventions in which it means octal.

Confusingly:
   $x=010   //$x is eight
   $x="010" //$x is ten


Please also correct the comment I added to the gmp_init page - I said that it 010 was parsed as hex, when I meant octal.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-20 06:44 UTC] bjori@php.net
What exactly gave you the impression it $i = 010; should 
be 10?
IMO it is very well documented at php.net/integer and has 
nothing to do GMP what so ever.

(I don't see your note anywhere..)
 [2006-09-20 10:06 UTC] php at richardneill dot org
Here's why I think it matters:

1) The documentation for gmp_init explicitly states:
 "String representation can be decimal or hexadecimal". 
which implies "...but not octal"

2) gmp_init uses strings, not numbers. However, PHP does different things with strings. See Code 1: I'd expect $c and $d to behave like $b. 

3) It's "common sense" that 010 means ten. I've been using PHP  daily for 5 years, and (mea culpa), I'd completely forgotten that PHP does octal. Of course, I'd never normally write integers that way, but see Code 2 for an example bug, which is specific to gmp, and where I initially omitted the ltrim(,0)

I also can't see my comment - I'll re-add it.


========== Code 1 ==========
<?
$a=010;
$b="010" + 0;
$c=gmp_strval(gmp_init(010));
$d=gmp_strval(gmp_init("010"));

echo "a: $a, b: $b, c: $c, d: $d\n";  
//prints a: 8, b: 10, c: 8, d: 8
//Expected: all 8, or all 10.
?>
==========================


========= Code 2 ==========
if (preg_match("/^[0-9]+\.[0-9]+$/",$input)){
   //Input is a base-10 decimal.
   $pieces=explode(".", $input);	
   //Multiply as necessary to remove the decimal point. 
   //Convert that to a gmp_resource, then decrement the 
   //exponent to compensate.
   $input="$pieces[0]$pieces[1]"; 
   //Remove the decimal point. 
   $input=ltrim($input,'0'); 
   //Then remove any leading zeros. We must remove leading
   //zeros, since otherwise, (and contrary to the
   //documentation), gmp_init will parse the number as 
   //octal!  Eg 0.25 becomes 025e-1 which becomes 21 * 10-1
   if ($input==''){
	$input=0;			
       //Fix the case of 0.0, or it would be ''
   }
   $input=gmp_init($input);
   $exponent=-strlen($pieces[1]);
   //Exponent is (-) the number of characters after 
   //the decimal point.
}

===========================
 [2006-09-20 17:38 UTC] bjori@php.net
You are right. It doesn't matter if you pass a string, 
decimal, octal or hex.. ..gmp changes it internally.

passing the _string_ "010" will be taken as 010 octal => 8 
decimal.

 [2006-10-09 23:32 UTC] pajoye@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

I just updated the documentation to reflect the real behavior of the gmp_init function. "010" is an octal number for gmp unless you specify the base to use.

From the GMP manual:
"The base may vary from 2 to 36. If base is 0, the actual base is determined from the leading characters: if the first two characters are "0x" or "0X", hexadecimal is assumed, otherwise if the first character is "0", octal is assumed, otherwise decimal is assumed."

Please note that the way gmp (the library) parses a string may differ from PHP. PHP itself has inconsistencies as you noticed.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 16:01:34 2025 UTC