php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35497 Difference between echo and printf
Submitted: 2005-11-30 22:51 UTC Modified: 2005-12-01 01:47 UTC
From: viperjason at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2005-11-30 (CVS) OS: Windows XP SP2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: viperjason at gmail dot com
New email:
PHP Version: OS:

 

 [2005-11-30 22:51 UTC] viperjason at gmail dot com
Description:
------------
If you look at the code I initialize a 32bit hex value and print it with both printf and echo.  I do a AND and a OR to get the value back to the original value and print it again with printf and echo.

Printf and echo disaggree on the first print
Printf and echo agree on the second print
Printf and echo agree on the third print.

What happened with the first print?
I realize you use signed integers.....but where is my sign?


Reproduce code:
---------------
<?
$a = 0x8000000F;
printf("hex=%x, dec=%d by printf\n",$a,$a);
echo $a."by echo\n";
$a = $a & 0x0FFFFFFF;
printf("hex=%x, dec=%d by printf\n",$a,$a);
echo $a."\n";
$a = $a | 0x80000000;
printf("hex=%x, dec=%d by printf\n",$a,$a);
echo $a."\n";
?>


Expected result:
----------------
I expect both printf and echo to consistantly print out the same decimal result.

Actual result:
--------------
The first time they didnt and second time they did.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-30 23:11 UTC] tony2001@php.net
>$a = 0x8000000F;
In this case $a is bigger than int, so the engine converts it to float automatically.
printf("%d") casts it back to int and you get negative value as the result.
echo (int)$a; will give you the same.
In all the other cases echo & printf() give the very same results.
No bug here.
 [2005-12-01 00:54 UTC] viperjason at gmail dot com
I thought ints were 32bits?  0x8000000F is a 32 bit value.
 [2005-12-01 01:12 UTC] tony2001@php.net
/usr/include/limits.h:
#define LONG_MAX 2147483647L

# php -r 'var_dump("0x".dechex(2147483647));'
string(10) "0x7fffffff"

# php -r 'var_dump(0x8000000F);'
float(2147483663)
 [2005-12-01 01:47 UTC] viperjason at gmail dot com
so in order to get a negative to display I have to cast it to an INT ?

var_dump(dechex(-1)) = 0xFFFFFFFF
var_dump(0xFFFFFFFF) = float(4294967295)
var_dump((int)0xFFFFFFFF) = int(-1)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 12:01:33 2025 UTC