sprintfDescriptionsprintf() is used to generate formatted strings from one or more arguments. The format argument is a string consisting of normal text and/or special conversion specifications. Conversion specifications begin with a single percent symbol (%). A sample format string might look like this: "There are %d days left to %s" The normal text in format is sent unmodified to the output of the function, while each conversion specification should be matched by an additional argument to sprintf() . Continuing the previous example, a complete call to sprintf() using the format string just described might look like this: $days = 5; $month = 'September'; echo sprintf("There are %d days left to %s", $days, $month); Outputs: There are 5 days left to September. The conversion specifiers are very powerful. They provide convenient ways to format or transform the value of their corresponding arguments; see the following paragraphs for a full description. Each conversion specifier starts with a single percent symbol%) and ends with a conversion character (one ofb, c,d,f,o,s, u,x, orX ). The specifier descriptions are described in the following table.
There may be additional conversion specifiers between the% and the conversion character. The following table lists the order in which they should be used within the conversion specifier.
Example<?php $values = array(75, -10, 'one-hundred'); $conversions = array('b', 'c', 'd', 'f', 'o', 's', 'u', 'x', 'X'); $options = array('', '12', '-12.4', "'x-12.4"); foreach($conversions as $conversion) { foreach($options as $option) { foreach($values as $value) { echo "\n$value processed with %$option$conversion:\n"; echo sprintf("%$option$conversion\n", $value); } } } ?> Output: 75 processed with %b: 1001011 -10 processed with %b: -1010 one-hundred processed with %b: 0 75 processed with %12b: 1001011 -10 processed with %12b: -1010 one-hundred processed with %12b: 0 75 processed with %-12.4b: -10 processed with %-12.4b: one-hundred processed with %-12.4b: 75 processed with %'x-12.4b: xxxxxxxxxxxx -10 processed with %'x-12.4b: xxxxxxxxxxxx one-hundred processed with %'x-12.4b: xxxxxxxxxxxx 75 processed with %c: K -10 processed with %c: ’ one-hundred processed with %c
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|