|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37538 toString in sprintf/printf and %d modifier does't work as expected
Submitted: 2006-05-21 05:40 UTC Modified: 2006-05-21 09:23 UTC
From: Assigned:
Status: Not a bug Package: Strings related
PHP Version: * OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2006-05-21 05:40 UTC]
The modifier %d doesn't work as one would expect sprintf/printf. When a string is returned, it is always replaced %d with '1' instead of the evaluated number of the __toString() output.

Consider this:

printf("%s %d", "4", "4");

will output "4 4", however if the parameters are replaced with an object whith magic __toString() which returns "4", %d is not replaced with 4 but with 1. It always returned 1, no matter what __toString() returns.

Reproduce code:
    class foo {
        function __toString() {
            return "4";
    printf("%s %d\n", new foo, new foo);
    printf("%s %d\n", "4", "4");

Expected result:
4 4
4 4

Actual result:
4 1
4 4


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-21 09:23 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

%d is an integer conversion "(int)$obj" as apposed to a string conversion that is served by __toString().
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Feb 29 04:01:27 2024 UTC