note 111201 added to function.sprintf

From: Date: Fri, 25 Jan 2013 08:17:02 +0000
Subject: note 111201 added to function.sprintf
Groups: php.notes 
Request: Send a blank email to php-notes+get-193386@lists.php.net to get a copy of this message
1. A plus sign ('+') means put a '+' before positive numbers while a minus sign ('-') means left justify. The documentation incorrectly states that they are interchangeable. They produce unique results that can be combined: <?php echo sprintf ("|%+4d|%+4d|\n", 1, -1); echo sprintf ("|%-4d|%-4d|\n", 1, -1); echo sprintf ("|%+-4d|%+-4d|\n", 1, -1); ?> outputs: | +1| -1| |1 |-1 | |+1 |-1 | 2. Padding with a '0' is different than padding with other characters. Zeros will only be added at the front of a number, after any sign. Other characters will be added before the sign, or after the number: <?php echo sprintf ("|%04d|\n", -2); echo sprintf ("|%':4d|\n", -2); echo sprintf ("|%-':4d|\n", -2); // Specifying both "-" and "0" creates a conflict with unexpected results: echo sprintf ("|%-04d|\n", -2); // Padding with other digits behaves like other non-zero characters: echo sprintf ("|%-'14d|\n", -2); echo sprintf ("|%-'04d|\n", -2); ?> outputs: |-002| |::-2| |-2::| |-2 | |-211| |-2 | ---- Server IP: 69.147.83.199 Probable Submitter: 174.30.190.64 ---- Manual Page -- http://php.net/manual/en/function.sprintf.php Edit -- https://master.php.net/note/edit/111201 Del: integrated -- https://master.php.net/note/delete/111201/integrated Del: useless -- https://master.php.net/note/delete/111201/useless Del: bad code -- https://master.php.net/note/delete/111201/bad+code Del: spam -- https://master.php.net/note/delete/111201/spam Del: non-english -- https://master.php.net/note/delete/111201/non-english Del: in docs -- https://master.php.net/note/delete/111201/in+docs Del: other reasons-- https://master.php.net/note/delete/111201 Reject -- https://master.php.net/note/reject/111201 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#193386) next »