Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 5846

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Return a formatted string</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.soundex.html">soundex</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.sscanf.html">sscanf</a></div>
 <div class="up"><a href="ref.strings.html">String Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.sprintf" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sprintf</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">sprintf</span> &mdash; <span class="dc-title">Return a formatted string</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.sprintf-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>sprintf</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$args</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Returns a string produced according to the formatting string
   <em><code class="parameter">format</code></em>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.sprintf-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">format</code></em></span>
     <dd>

      <p class="para">
       The format string is composed of zero or more directives:
       ordinary characters (excluding <em>%</em>) that are
       copied directly to the result, and <em class="emphasis">conversion
       specifications</em>, each of which results in fetching its
       own parameter.  This applies to both  <span class="function"><strong>sprintf()</strong></span>
       and  <span class="function"><a href="function.printf.html" class="function">printf()</a></span>.
      </p>
      <p class="para">
       Each conversion specification consists of a percent sign
       (<em>%</em>), followed by one or more of these
       elements, in order:
       <ol type="1">
        <li class="listitem">
         <span class="simpara">
          An optional <em class="emphasis">sign specifier</em> that forces a sign
          (- or +) to be used on a number. By default, only the - sign is used
          on a number if it&#039;s negative. This specifier forces positive numbers
          to have the + sign attached as well, and was added in PHP 4.3.0.
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          An optional <em class="emphasis">padding specifier</em> that says
          what character will be used for padding the results to the
          right string size.  This may be a space character or a
          <em>0</em> (zero character).  The default is to pad
          with spaces.  An alternate padding character can be specified
          by prefixing it with a single quote (<em>&#039;</em>).
          See the examples below.
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          An optional <em class="emphasis">alignment specifier</em> that says
          if the result should be left-justified or right-justified.
          The default is right-justified; a <em>-</em>
          character here will make it left-justified.
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          An optional number, a <em class="emphasis">width specifier</em>
          that says how many characters (minimum) this conversion should
          result in.
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          An optional <em class="emphasis">precision specifier</em> in the form
          of a period (`.&#039;) followed by an optional decimal digit string
          that says how many decimal digits should be displayed for
          floating-point numbers. When using this specifier on a string,
          it acts as a cutoff point, setting a maximum character limit to
          the string.
         </span>
        </li>
        <li class="listitem">
         <p class="para">
          A <em class="emphasis">type specifier</em> that says what type the
          argument data should be treated as.  Possible types:
          <ul class="simplelist">
           <li class="member">
            <em>%</em> - a literal percent character. No
            argument is required.
           </li>
           <li class="member">
            <em>b</em> - the argument is treated as an
            integer, and presented as a binary number.
           </li>
           <li class="member">
            <em>c</em> - the argument is treated as an
            integer, and presented as the character with that ASCII
            value.
           </li>
           <li class="member">
            <em>d</em> - the argument is treated as an
            integer, and presented as a (signed) decimal number.
           </li>
           <li class="member">
            <em>e</em> - the argument is treated as scientific
            notation (e.g. 1.2e+2).
            The precision specifier stands for the number of digits after the
            decimal point since PHP 5.2.1. In earlier versions, it was taken as
            number of significant digits (one less).
           </li>
           <li class="member">
            <em>E</em> - like <em>%e</em> but uses
            uppercase letter (e.g. 1.2E+2).
           </li>
           <li class="member">
            <em>f</em> - the argument is treated as a
            float, and presented as a floating-point number (locale aware).
           </li>
           <li class="member">
            <em>F</em> - the argument is treated as a
            float, and presented as a floating-point number (non-locale aware).
            Available since PHP 4.3.10 and PHP 5.0.3.
           </li>
           <li class="member">
            <em>g</em> - shorter of <em>%e</em> and
            <em>%f</em>.
           </li>
           <li class="member">
            <em>G</em> - shorter of <em>%E</em> and
            <em>%f</em>.
           </li>
           <li class="member">
            <em>o</em> - the argument is treated as an
            integer, and presented as an octal number.
           </li>
           <li class="member">
            <em>s</em> - the argument is treated as and
            presented as a string.
           </li>
           <li class="member">
            <em>u</em> - the argument is treated as an
            integer, and presented as an unsigned decimal number.
           </li>
           <li class="member">
            <em>x</em> - the argument is treated as an integer
            and presented as a hexadecimal number (with lowercase
            letters).
           </li>
           <li class="member">
            <em>X</em> - the argument is treated as an integer
            and presented as a hexadecimal number (with uppercase
            letters).
           </li>
          </ul>
         </p>
        </li>
       </ol>
      </p>
      <p class="para">
       Variables will be co-erced to a suitable type for the specifier:
       <table id="sprintf.coercion" class="doctable table">
        <caption><strong>Type Handling</strong></caption>
        
         <thead>
          <tr>
           <th>Type</th>
           <th>Specifiers</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td><em>string</em></td>
           <td><em>s</em></td>
          </tr>

          <tr>
           <td><em>integer</em></td>
           <td>
            <em>d</em>, 
            <em>u</em>, 
            <em>c</em>,
            <em>o</em>,
            <em>x</em>,
            <em>X</em>,
            <em>b</em>
           </td>
          </tr>

          <tr>
           <td><em>double</em></td>
           <td>
            <em>g</em>,
            <em>G</em>,
            <em>e</em>,
            <em>E</em>,
            <em>f</em>,
            <em>F</em>
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
      <div class="warning"><strong class="warning">Warning</strong>
       <p class="para">
        Attempting to use a combination of the string and width specifiers with character sets that require more than one byte per character may result in unexpected results
       </p>
      </div>
      <p class="para">
       The format string supports argument numbering/swapping.  Here is an
       example:
       <div class="example" id="example-4863">
        <p><strong>Example #1 Argument swapping</strong></p>
        <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$num&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$location&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'tree'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'There&nbsp;are&nbsp;%d&nbsp;monkeys&nbsp;in&nbsp;the&nbsp;%s'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

       </div>
       This will output &quot;There are 5 monkeys in the tree&quot;.  But
       imagine we are creating a format string in a separate file,
       commonly because we would like to internationalize it and we
       rewrite it as:
       <div class="example" id="example-4864">
        <p><strong>Example #2 Argument swapping</strong></p>
        <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'The&nbsp;%s&nbsp;contains&nbsp;%d&nbsp;monkeys'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

       </div>
       We now have a problem.  The order of the placeholders in the
       format string does not match the order of the arguments in the
       code.  We would like to leave the code as is and simply indicate
       in the format string which arguments the placeholders refer to.
       We would write the format string like this instead:
       <div class="example" id="example-4865">
        <p><strong>Example #3 Argument swapping</strong></p>
        <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'The&nbsp;%2$s&nbsp;contains&nbsp;%1$d&nbsp;monkeys'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

       </div>
       An added benefit here is that you can repeat the placeholders without
       adding more arguments in the code.  For example:
       <div class="example" id="example-4866">
        <p><strong>Example #4 Argument swapping</strong></p>
        <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'The&nbsp;%2$s&nbsp;contains&nbsp;%1$d&nbsp;monkeys.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;That\'s&nbsp;a&nbsp;nice&nbsp;%2$s&nbsp;full&nbsp;of&nbsp;%1$d&nbsp;monkeys.'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

       </div>
       When using argument swapping, the <em>n$</em>
       <em class="emphasis">position specifier</em> must come immediately
       after the percent sign (<em>%</em>), before any other
       specifiers, as shown in the example below.
       <div class="example" id="example-4867">
        <p><strong>Example #5 Position specifier with other specifiers</strong></p>
        <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'The&nbsp;%2$s&nbsp;contains&nbsp;%1$04d&nbsp;monkeys'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

        <div class="example-contents"><p>The above example will output:</p></div>
        <div class="example-contents screen">
<div class="cdata"><pre>
The tree contains 0005 monkeys
</pre></div>
        </div>
       </div>
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        Attempting to use a position specifier greater than
        <strong><code>PHP_INT_MAX</code></strong> will result in
         <span class="function"><strong>sprintf()</strong></span> generating warnings.
       </p>
      </p></blockquote>
      <div class="warning"><strong class="warning">Warning</strong>
       <p class="para">
        The <em>c</em> type specifier ignores padding and width
       </p>
      </div>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">args</code></em></span>
     <dd>

      <p class="para">
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">...</code></em></span>
     <dd>

      <p class="para">
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sprintf-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns a string produced according to the formatting string
   <em><code class="parameter">format</code></em>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.sprintf-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.0.6</td>
       <td>
        Support for argument numbering/swapping was added
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.sprintf-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="example-4868">
   <p><strong>Example #6  <span class="function"><a href="function.printf.html" class="function">printf()</a></span>: various examples</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$n&nbsp;</span><span style="color: #007700">=&nbsp;&nbsp;</span><span style="color: #0000BB">43951789</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$u&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">43951789</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">65</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;ASCII&nbsp;65&nbsp;is&nbsp;'A'<br /><br />//&nbsp;notice&nbsp;the&nbsp;double&nbsp;%%,&nbsp;this&nbsp;prints&nbsp;a&nbsp;literal&nbsp;'%'&nbsp;character<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%b&nbsp;=&nbsp;'%b'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;binary&nbsp;representation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%c&nbsp;=&nbsp;'%c'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;print&nbsp;the&nbsp;ascii&nbsp;character,&nbsp;same&nbsp;as&nbsp;chr()&nbsp;function<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%d&nbsp;=&nbsp;'%d'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;standard&nbsp;integer&nbsp;representation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%e&nbsp;=&nbsp;'%e'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;scientific&nbsp;notation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%u&nbsp;=&nbsp;'%u'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;unsigned&nbsp;integer&nbsp;representation&nbsp;of&nbsp;a&nbsp;positive&nbsp;integer<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%u&nbsp;=&nbsp;'%u'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$u</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;unsigned&nbsp;integer&nbsp;representation&nbsp;of&nbsp;a&nbsp;negative&nbsp;integer<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%f&nbsp;=&nbsp;'%f'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;floating&nbsp;point&nbsp;representation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%o&nbsp;=&nbsp;'%o'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;octal&nbsp;representation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%s&nbsp;=&nbsp;'%s'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;string&nbsp;representation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%x&nbsp;=&nbsp;'%x'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;hexadecimal&nbsp;representation&nbsp;(lower-case)<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%X&nbsp;=&nbsp;'%X'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;hexadecimal&nbsp;representation&nbsp;(upper-case)<br /><br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%+d&nbsp;=&nbsp;'%+d'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;sign&nbsp;specifier&nbsp;on&nbsp;a&nbsp;positive&nbsp;integer<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%+d&nbsp;=&nbsp;'%+d'\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$u</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;sign&nbsp;specifier&nbsp;on&nbsp;a&nbsp;negative&nbsp;integer<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
%b = &#039;10100111101010011010101101&#039;
%c = &#039;A&#039;
%d = &#039;43951789&#039;
%e = &#039;4.39518e+7&#039;
%u = &#039;43951789&#039;
%u = &#039;4251015507&#039;
%f = &#039;43951789.000000&#039;
%o = &#039;247523255&#039;
%s = &#039;43951789&#039;
%x = &#039;29ea6ad&#039;
%X = &#039;29EA6AD&#039;
%+d = &#039;+43951789&#039;
%+d = &#039;-43951789&#039;
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4869">
   <p><strong>Example #7  <span class="function"><a href="function.printf.html" class="function">printf()</a></span>: string specifiers</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$s&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'monkey'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$t&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'many&nbsp;monkeys'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%s]\n"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;standard&nbsp;string&nbsp;output<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%10s]\n"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;right-justification&nbsp;with&nbsp;spaces<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%-10s]\n"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;left-justification&nbsp;with&nbsp;spaces<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%010s]\n"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;zero-padding&nbsp;works&nbsp;on&nbsp;strings&nbsp;too<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%'#10s]\n"</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;use&nbsp;the&nbsp;custom&nbsp;padding&nbsp;character&nbsp;'#'<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%10.10s]\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$t</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;left-justification&nbsp;but&nbsp;with&nbsp;a&nbsp;cutoff&nbsp;of&nbsp;10&nbsp;characters<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
[monkey]
[    monkey]
[monkey    ]
[0000monkey]
[####monkey]
[many monke]
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4870">
   <p><strong>Example #8  <span class="function"><strong>sprintf()</strong></span>: zero-padded integers</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$isodate&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%04d-%02d-%02d"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$day</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <div class="example" id="example-4871">
   <p><strong>Example #9  <span class="function"><strong>sprintf()</strong></span>: formatting currency</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$money1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">68.75</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$money2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">54.35</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$money&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$money1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$money2</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;echo&nbsp;$money&nbsp;will&nbsp;output&nbsp;"123.1";<br /></span><span style="color: #0000BB">$formatted&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%01.2f"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$money</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;echo&nbsp;$formatted&nbsp;will&nbsp;output&nbsp;"123.10"<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <div class="example" id="example-4872">
   <p><strong>Example #10  <span class="function"><strong>sprintf()</strong></span>: scientific notation</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">362525200</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%.3e"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;outputs&nbsp;3.625e+8<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.sprintf-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - Output a formatted string</span></li>
    <li class="member"> <span class="function"><a href="function.sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a> - Parses input from a string according to a format</span></li>
    <li class="member"> <span class="function"><a href="function.fscanf.html" class="function" rel="rdfs-seeAlso">fscanf()</a> - Parses input from a file according to a format</span></li>
    <li class="member"> <span class="function"><a href="function.vsprintf.html" class="function" rel="rdfs-seeAlso">vsprintf()</a> - Return a formatted string</span></li>
    <li class="member"> <span class="function"><a href="function.number-format.html" class="function" rel="rdfs-seeAlso">number_format()</a> - Format a number with grouped thousands</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.soundex.html">soundex</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.sscanf.html">sscanf</a></div>
 <div class="up"><a href="ref.strings.html">String Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>