#37538 [Opn->Bgs]: toString in sprintf/printf and %d modifier does't work as expected

From: Date: Sun, 21 May 2006 09:23:46 +0000
Subject: #37538 [Opn->Bgs]: toString in sprintf/printf and %d modifier does't work as expected
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-97422@lists.php.net to get a copy of this message
ID: 37538 Updated by: helly@php.net Reported By: mfischer@php.net -Status: Open +Status: Bogus Bug Type: Strings related -Operating System: Linux +Operating System: * -PHP Version: 5CVS-2006-05-21 (CVS) +PHP Version: * New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php %d is an integer conversion "(int)$obj" as apposed to a string conversion that is served by __toString(). Previous Comments: ------------------------------------------------------------------------ [2006-05-21 05:40:36] mfischer@php.net Description: ------------ 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=37538&edit=1

« previous php.bugs (#97422) next »