note 124412 added to function.sprintf
| From: | Anderson@php.net | Date: | Mon, 18 Nov 2019 21:23:49 +0000 |
| Subject: | note 124412 added to function.sprintf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-213148@lists.php.net to get a copy of this message | ||
The old "monkey" example which helped me a lot has sadly disappeared.
I'll Re-post it in comment as a memory.
<?php
$n = 43951789;
$u = -43951789;
$c = 65; // ASCII 65 is 'A'
// notice the double %%, this prints a literal '%' character
printf("%%b = '%b'\n", $n); // binary representation
printf("%%c = '%c'\n", $c); // print the ascii character, same as chr() function
printf("%%d = '%d'\n", $n); // standard integer representation
printf("%%e = '%e'\n", $n); // scientific notation
printf("%%u = '%u'\n", $n); // unsigned integer representation of a positive
integer
printf("%%u = '%u'\n", $u); // unsigned integer representation of a negative
integer
printf("%%f = '%f'\n", $n); // floating point representation
printf("%%o = '%o'\n", $n); // octal representation
printf("%%s = '%s'\n", $n); // string representation
printf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case)
printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case)
printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integer
printf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer
/*
%b = '10100111101010011010101101'
%c = 'A'
%d = '43951789'
%e = '4.395179e+7'
%u = '43951789'
%u = '18446744073665599827'
%f = '43951789.000000'
%o = '247523255'
%s = '43951789'
%x = '29ea6ad'
%X = '29EA6AD'
%+d = '+43951789'
%+d = '-43951789'
*/
$s = 'monkey';
$t = 'many monkeys';
printf("[%s]\n", $s); // standard string output
printf("[%10s]\n", $s); // right-justification with spaces
printf("[%-10s]\n", $s); // left-justification with spaces
printf("[%010s]\n", $s); // zero-padding works on strings too
printf("[%'#10s]\n", $s); // use the custom padding character '#'
printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters
/*
[monkey]
[ monkey]
[monkey ]
[0000monkey]
[####monkey]
[many monke]
*/
?>
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.168 (proxied: 39.83.129.242)
----
Manual Page -- http://php.net/manual/en/function.sprintf.php
Edit -- https://master.php.net/note/edit/124412
Del: integrated -- https://master.php.net/note/delete/124412/integrated
Del: useless -- https://master.php.net/note/delete/124412/useless
Del: bad code -- https://master.php.net/note/delete/124412/bad+code
Del: spam -- https://master.php.net/note/delete/124412/spam
Del: non-english -- https://master.php.net/note/delete/124412/non-english
Del: in docs -- https://master.php.net/note/delete/124412/in+docs
Del: other reasons-- https://master.php.net/note/delete/124412
Reject -- https://master.php.net/note/reject/124412
Search -- https://master.php.net/manage/user-notes.php