<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.strings.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.sprintf.php',
    1 => 'sprintf',
    2 => 'Return a formatted string',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String Functions',
  ),
  'prev' => 
  array (
    0 => 'function.soundex.php',
    1 => 'soundex',
  ),
  'next' => 
  array (
    0 => 'function.sscanf.php',
    1 => 'sscanf',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/strings/functions/sprintf.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.sprintf" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sprintf</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</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="methodname"><strong>sprintf</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$format</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">...$values</code></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

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


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

 <dt><code class="parameter">format</code></dt>
 <dd>
  <p class="para">
   The format string is composed of zero or more directives:
   ordinary characters (excluding <code class="literal">%</code>) that are
   copied directly to the result and <em>conversion
   specifications</em>, each of which results in fetching its
   own parameter.
  </p>

  <p class="para">
   A conversion specification follows this prototype:
   <code class="literal">%[argnum$][flags][width][.precision]specifier</code>.
  </p>

  <p class="formalpara">
   <h5 class="title">Argnum</h5>
   <p class="para">
    An integer followed by a dollar sign <code class="literal">$</code>,
    to specify which number argument to treat in the conversion.
   </p>
  </p>

  <p class="formalpara">
   <h5 class="title">Flags</h5>
   <p class="para">
    <table class="doctable informaltable">
     
      <thead>
       <tr>
        <th>Flag</th>
        <th>Description</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td><code class="literal">-</code></td>
        <td>
         Left-justify within the given field width;
         Right justification is the default
        </td>
       </tr>

       <tr>
        <td><code class="literal">+</code></td>
        <td>
         Prefix positive numbers with a plus sign
         <code class="literal">+</code>; Default only negative
         are prefixed with a negative sign.
        </td>
       </tr>

       <tr>
        <td><code class="literal"> </code>(space)</td>
        <td>
         Pads the result with spaces.
         This is the default.
        </td>
       </tr>

       <tr>
        <td><code class="literal">0</code></td>
        <td>
         Only left-pads numbers with zeros.
         With <code class="literal">s</code> specifiers this can
         also right-pad with zeros.
        </td>
       </tr>

       <tr>
        <td><code class="literal">&#039;</code>(char)</td>
        <td>
         Pads the result with the character (char).
        </td>
       </tr>

      </tbody>
     
    </table>

   </p>
  </p>

  <p class="formalpara">
   <h5 class="title">Width</h5>
   <p class="para">
    Either an integer that says how many characters (minimum)
    this conversion should result in, or <code class="literal">*</code>.
    If <code class="literal">*</code> is used, then the width is supplied
    as an additional integer value preceding the one formatted
    by the specifier.
   </p>
  </p>

  <p class="formalpara">
   <h5 class="title">Precision</h5>
   <p class="para">
    A period <code class="literal">.</code> optionally followed by
    either an integer or <code class="literal">*</code>,
    whose meaning depends on the specifier:
    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       For <code class="literal">e</code>, <code class="literal">E</code>,
       <code class="literal">f</code> and <code class="literal">F</code>
       specifiers: this is the number of digits to be printed
       after the decimal point (by default, this is 6).
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       For <code class="literal">g</code>, <code class="literal">G</code>,
       <code class="literal">h</code> and <code class="literal">H</code>
       specifiers: this is the maximum number of significant
       digits to be printed.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       For <code class="literal">s</code> specifier: it acts as a cutoff point,
       setting a maximum character limit to the string.
      </span>
     </li>
    </ul>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      If the period is specified without an explicit value for precision,
      0 is assumed. If <code class="literal">*</code> is used, the precision is
      supplied as an additional integer value preceding the one formatted
      by the specifier.
     </span>
    </p></blockquote>
   </p>
  </p>

  <p class="para">
   <table class="doctable table">
    <caption><strong>Specifiers</strong></caption>
    
     <thead>
      <tr>
       <th>Specifier</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><code class="literal">%</code></td>
       <td>
        A literal percent character. No argument is required.
       </td>
      </tr>

      <tr>
       <td><code class="literal">b</code></td>
       <td>
        The argument is treated as an integer and presented
        as a binary number.
       </td>
      </tr>

      <tr>
       <td><code class="literal">c</code></td>
       <td>
        The argument is treated as an integer and presented
        as the character with that ASCII.
       </td>
      </tr>

      <tr>
       <td><code class="literal">d</code></td>
       <td>
        The argument is treated as an integer and presented
        as a (signed) decimal number.
       </td>
      </tr>

      <tr>
       <td><code class="literal">e</code></td>
       <td>
        The argument is treated as scientific notation (e.g. 1.2e+2).
       </td>
      </tr>

      <tr>
       <td><code class="literal">E</code></td>
       <td>
        Like the <code class="literal">e</code> specifier but uses
        uppercase letter (e.g. 1.2E+2).
       </td>
      </tr>

      <tr>
       <td><code class="literal">f</code></td>
       <td>
        The argument is treated as a float and presented
        as a floating-point number (locale aware).
       </td>
      </tr>

      <tr>
       <td><code class="literal">F</code></td>
       <td>
        The argument is treated as a float and presented
        as a floating-point number (non-locale aware).
       </td>
      </tr>

      <tr>
       <td><code class="literal">g</code></td>
       <td>
        <p class="para">
         General format.
        </p>
        <p class="para">
         Let P equal the precision if nonzero, 6 if the precision is omitted,
         or 1 if the precision is zero.
         Then, if a conversion with style E would have an exponent of X:
        </p>
        <p class="para">
         If P &gt; X ≥ −4, the conversion is with style f and precision P − (X + 1).
         Otherwise, the conversion is with style e and precision P − 1.
        </p>
       </td>
      </tr>

      <tr>
       <td><code class="literal">G</code></td>
       <td>
        Like the <code class="literal">g</code> specifier but uses
        <code class="literal">E</code> and <code class="literal">f</code>.
       </td>
      </tr>

      <tr>
       <td><code class="literal">h</code></td>
       <td>
        Like the <code class="literal">g</code> specifier but uses <code class="literal">F</code>.
        Available as of PHP 8.0.0.
       </td>
      </tr>

      <tr>
       <td><code class="literal">H</code></td>
       <td>
        Like the <code class="literal">g</code> specifier but uses
        <code class="literal">E</code> and <code class="literal">F</code>. Available as of PHP 8.0.0.
       </td>
      </tr>

      <tr>
       <td><code class="literal">o</code></td>
       <td>
        The argument is treated as an integer and presented
        as an octal number.
       </td>
      </tr>

      <tr>
       <td><code class="literal">s</code></td>
       <td>
        The argument is treated and presented as a string.
       </td>
      </tr>

      <tr>
       <td><code class="literal">u</code></td>
       <td>
        The argument is treated as an integer and presented
        as an unsigned decimal number.
       </td>
      </tr>

      <tr>
       <td><code class="literal">x</code></td>
       <td>
        The argument is treated as an integer and presented
        as a hexadecimal number (with lowercase letters).
       </td>
      </tr>

      <tr>
       <td><code class="literal">X</code></td>
       <td>
        The argument is treated as an integer and presented
        as a hexadecimal number (with uppercase letters).
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>

  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    The <code class="literal">c</code> type specifier ignores padding and width.
   </p>
  </div>

  <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">
   Variables will be co-erced to a suitable type for the specifier:
   <table class="doctable table">
    <caption><strong>Type Handling</strong></caption>
    
     <thead>
      <tr>
       <th>Type</th>
       <th>Specifiers</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td>
       <td><code class="literal">s</code></td>
      </tr>

      <tr>
       <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
       <td>
        <code class="literal">d</code>,
        <code class="literal">u</code>,
        <code class="literal">c</code>,
        <code class="literal">o</code>,
        <code class="literal">x</code>,
        <code class="literal">X</code>,
        <code class="literal">b</code>
       </td>
      </tr>

      <tr>
       <td><span class="type"><a href="language.types.float.php" class="type float">float</a></span></td>
       <td>
        <code class="literal">e</code>,
        <code class="literal">E</code>,
        <code class="literal">f</code>,
        <code class="literal">F</code>,
        <code class="literal">g</code>,
        <code class="literal">G</code>,
        <code class="literal">h</code>,
        <code class="literal">H</code>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </dd>


    
     <dt><code class="parameter">values</code></dt>
     <dd>
      <p class="para">
      </p>
     </dd>
    
   </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
   <code class="parameter">format</code>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.sprintf-errors">
  <h3 class="title">Errors/Exceptions</h3>
  
  <p class="para">
   As of PHP 8.0.0, a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is thrown if the number of arguments is zero.
   Prior to PHP 8.0.0, a <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was emitted instead.
  </p>
  <p class="para">
   As of PHP 8.0.0, a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is thrown if <code class="literal">[width]</code> is less than zero or bigger than <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>.
   Prior to PHP 8.0.0, a <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was emitted instead.
  </p>
  <p class="para">
   As of PHP 8.0.0, a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is thrown if <code class="literal">[precision]</code> is less than zero or bigger than <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>.
   Prior to PHP 8.0.0, a <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was emitted instead.
  </p>
  <p class="para">
   As of PHP 8.0.0, a <span class="classname"><a href="class.argumentcounterror.php" class="classname">ArgumentCountError</a></span> is thrown when less arguments are given than required.
   Prior to PHP 8.0.0, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> was returned and a <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> emitted instead.
  </p>

 </div>


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

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       This function no longer returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Throw a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> if the number of arguments is zero;
       previously this function emitted a <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> instead.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Throw a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> if <code class="literal">[width]</code> is less than zero or bigger than <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>;
       previously this function emitted a <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> instead.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Throw a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> if <code class="literal">[precision]</code> is less than zero or bigger than <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>;
       previously this function emitted a <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> instead.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Throw a <span class="classname"><a href="class.argumentcounterror.php" class="classname">ArgumentCountError</a></span> when less arguments are given than required;
       previously this function emitted a <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> instead.
      </td>
     </tr>

    </tbody>
   
  </table>


 </div>


 <div class="refsect1 examples" id="refsect1-function.sprintf-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 Argument swapping</strong></p>
   <div class="example-contents"><p>
    The format string supports argument numbering/swapping.
   </p></div>
   <div class="example-contents">
    <div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$num </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$location </span><span style="color: #007700">= </span><span style="color: #DD0000">'tree'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'There are %d monkeys in the %s'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">, </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="annotation-interactive examplescode"><pre class="examplescode">There are 5 monkeys in the tree</pre>
</div>
   </div>
  </div>
  <p class="para">
    However imagine we are creating a format string in a separate file,
    commonly because we would like to internationalize it and we rewrite it as:
  </p>
  <div class="example" id="example-2">
   <p><strong>Example #2 Wrong Argument Order</strong></p>
   <div class="example-contents"><p>
    The format string supports argument numbering/swapping.
   </p></div>
   <div class="example-contents">
    <div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$num </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$location </span><span style="color: #007700">= </span><span style="color: #DD0000">'tree'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'The %s contains %d monkeys'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">, </span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <p class="para">
   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:
  </p>

  <div class="example" id="example-3">
   <p><strong>Example #3 Use Order Placeholder</strong></p>
   <div class="example-contents">
    <div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$num </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$location </span><span style="color: #007700">= </span><span style="color: #DD0000">'tree'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'The %2$s contains %1$d monkeys'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">, </span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   An added benefit is that placeholders can be repeated without adding
   more arguments in the code.
  </p>

  <div class="example" id="example-4">
   <p><strong>Example #4 Repeated Placeholder</strong></p>
   <div class="example-contents">
    <div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$num </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$location </span><span style="color: #007700">= </span><span style="color: #DD0000">'tree'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'The %2$s contains %1$d monkeys.<br />           That\'s a nice %2$s full of %1$d monkeys.'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">, </span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   When using argument swapping, the <code class="literal">n$</code>
   <em>position specifier</em> must come immediately
   after the percent sign (<code class="literal">%</code>), before any other
   specifiers, as shown below.
  </p>

  <div class="example" id="example-5">
   <p><strong>Example #5 Specifying padding character</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%'.9d\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">123</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%'.09d\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">123</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="annotation-interactive examplescode"><pre class="examplescode">......123
000000123</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-6">
   <p><strong>Example #6 Position specifier with other specifiers</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$num </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$location </span><span style="color: #007700">= </span><span style="color: #DD0000">'tree'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'The %2$s contains %1$04d monkeys'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">, </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="annotation-interactive examplescode"><pre class="examplescode">The tree contains 0005 monkeys</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-7">
   <p><strong>Example #7 <span class="function"><strong>sprintf()</strong></span>: zero-padded integers</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$year </span><span style="color: #007700">= </span><span style="color: #0000BB">2005</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$month </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$day </span><span style="color: #007700">= </span><span style="color: #0000BB">6</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$isodate </span><span style="color: #007700">= </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%04d-%02d-%02d"</span><span style="color: #007700">, </span><span style="color: #0000BB">$year</span><span style="color: #007700">, </span><span style="color: #0000BB">$month</span><span style="color: #007700">, </span><span style="color: #0000BB">$day</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$isodate</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <div class="example" id="example-8">
   <p><strong>Example #8 <span class="function"><strong>sprintf()</strong></span>: formatting currency</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$money1 </span><span style="color: #007700">= </span><span style="color: #0000BB">68.75</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$money2 </span><span style="color: #007700">= </span><span style="color: #0000BB">54.35</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$money </span><span style="color: #007700">= </span><span style="color: #0000BB">$money1 </span><span style="color: #007700">+ </span><span style="color: #0000BB">$money2</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$money</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$formatted </span><span style="color: #007700">= </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%01.2f"</span><span style="color: #007700">, </span><span style="color: #0000BB">$money</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$formatted</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</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="annotation-interactive examplescode"><pre class="examplescode">123.1
123.10</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-9">
   <p><strong>Example #9 <span class="function"><strong>sprintf()</strong></span>: scientific notation</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$number </span><span style="color: #007700">= </span><span style="color: #0000BB">362525200</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%.3e"</span><span style="color: #007700">, </span><span style="color: #0000BB">$number</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</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="annotation-interactive examplescode"><pre class="examplescode">3.625e+8</pre>
</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><span class="function"><a href="function.printf.php" class="function" rel="rdfs-seeAlso">printf()</a> - Output a formatted string</span></li>
    <li><span class="function"><a href="function.fprintf.php" class="function" rel="rdfs-seeAlso">fprintf()</a> - Write a formatted string to a stream</span></li>
    <li><span class="function"><a href="function.vprintf.php" class="function" rel="rdfs-seeAlso">vprintf()</a> - Output a formatted string</span></li>
    <li><span class="function"><a href="function.vsprintf.php" class="function" rel="rdfs-seeAlso">vsprintf()</a> - Return a formatted string</span></li>
    <li><span class="function"><a href="function.vfprintf.php" class="function" rel="rdfs-seeAlso">vfprintf()</a> - Write a formatted string to a stream</span></li>
    <li><span class="function"><a href="function.sscanf.php" class="function" rel="rdfs-seeAlso">sscanf()</a> - Parses input from a string according to a format</span></li>
    <li><span class="function"><a href="function.fscanf.php" class="function" rel="rdfs-seeAlso">fscanf()</a> - Parses input from a file according to a format</span></li>
    <li><span class="function"><a href="function.number-format.php" class="function" rel="rdfs-seeAlso">number_format()</a> - Format a number with grouped thousands</span></li>
    <li><span class="function"><a href="function.date.php" class="function" rel="rdfs-seeAlso">date()</a> - Format a Unix timestamp</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>