|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43690 sprintf returning unexpected number
Submitted: 2007-12-27 16:24 UTC Modified: 2007-12-29 12:30 UTC
From: yardgnomeray at gmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.5 OS: Windows XP SP2
Private report: No CVE-ID: None
 [2007-12-27 16:24 UTC] yardgnomeray at gmail dot com
Using integer >= 2147483647 for a %u or %d will return 2147483647

Reproduce code:
$ip = "";
$ipaddr = sprintf("%u",ip2long($ip));
$address = sprintf("IP ADDR = %u", $ipaddr);
echo $address;

Expected result:
IP ADDR = 3574256784

Actual result:
IP ADDR = 2147483647


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-28 14:45 UTC]
ip2long() returns a string. Converting this value to float for obtain large precision, you will have the expected result.

$ip = "";
$ipaddr = sprintf("%u", ip2long($ip));
$address = sprintf("IP ADDR = %u", (float) $ipaddr);

var_dump(PHP_INT_MAX, ip2long($ip), $ipaddr, $address);

string(10) "3574256784"
string(20) "IP ADDR = 3574256784"
 [2007-12-28 14:46 UTC]
Ops, ignore the part "ip2long() returns a string."
 [2007-12-29 10:22 UTC]
@felipe... you're wrong here - this works just fine without having to cast to float:

derick@kossu:~$ php-5.2.5RC2 

$ip = "";
$ipaddr = sprintf("%u",ip2long($ip));
$address = sprintf("IP ADDR = %u", $ipaddr);
echo $address;

IP ADDR = 3574256784d
 [2007-12-29 12:15 UTC]
I'm using PHP 5.3.0-dev (cli) (built: Dec 25 2007 10:07:55), and the convertion was necessary.
 [2007-12-29 12:30 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Nothing is wrong here:

$ip = \"\";
var_dump(ip2long( $ip ) );

returns: int(-720710512)

$ip = \"\";
$ipaddr = sprintf(\"%u\",ip2long($ip));
var_dump( $ipaddr );

returns: string(10) \"3574256784\"

Using a string that does not fit in the integer range as a number (with %d or %u) does not work, as the string itself is first converted to an integer - which won\'t work as it doesn\'t fit. You shouldn\'t simply use %u for the returned variable from sprintf() - as it\'s not a n integer, but a string (so use %s).

PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Mon Jun 05 19:03:38 2023 UTC