<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.vfprintf.php',
    1 => 'vfprintf',
    2 => '将格式化字符串写入流',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => '字符串 函数',
  ),
  'prev' => 
  array (
    0 => 'function.utf8-encode.php',
    1 => 'utf8_encode',
  ),
  'next' => 
  array (
    0 => 'function.vprintf.php',
    1 => 'vprintf',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/strings/functions/vfprintf.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.vfprintf" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">vfprintf</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">vfprintf</span> &mdash; <span class="dc-title">将格式化字符串写入流</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.vfprintf-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>vfprintf</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$stream</code></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.array.php" class="type array">array</a></span> <code class="parameter">$values</code></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment">
   向由 <code class="parameter">stream</code> 指定的流资源句柄中写入根据 <code class="parameter">format</code> 格式化后的字符串。
  </p>
  <p class="para">
   作用与 <span class="function"><a href="function.fprintf.php" class="function">fprintf()</a></span> 函数类似，但是接收一个数组参数，而不是一系列可变数量的参数。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.vfprintf-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">stream</code></dt>
     <dd>
      <p class="para">
      </p>
     </dd>
    
    

 <dt><code class="parameter">format</code></dt>
 <dd>
  <p class="para">
   format 字符串通常由零或多个指令组成：普通字符（不包含
   <code class="literal">%</code>）——直接复制到结果，<em>转换规范</em>——获取每个参数的结果。
  </p>

  <p class="para">
   转换规范遵循此原型：
   <code class="literal">%[argnum$][flags][width][.precision]specifier</code>.
  </p>

  <p class="formalpara">
   <h5 class="title">Argnum</h5>
   <p class="para">
    整数后跟美元符号 <code class="literal">$</code>，用于指定转换中要处理的参数数量。
   </p>
  </p>

  <p class="formalpara">
   <h5 class="title">标志</h5>
   <p class="para">
    <table class="doctable informaltable">
     
      <thead>
       <tr>
        <th>标志</th>
        <th>说明</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td><code class="literal">-</code></td>
        <td>
         在给定字段宽度内左对齐；右对齐是默认值
        </td>
       </tr>

       <tr>
        <td><code class="literal">+</code></td>
        <td>
         在给定字段宽度内为正数添加加号前缀
         <code class="literal">+</code>；默认情况下，只有负数才会添加负号前缀。
        </td>
       </tr>

       <tr>
        <td><code class="literal"> </code>(space)</td>
        <td>
         用空格填充结果。
         这是默认值。
        </td>
       </tr>

       <tr>
        <td><code class="literal">0</code></td>
        <td>
         仅对数字进行左侧零填充。
         对于 <code class="literal">s</code> 说明符，也可以右侧用零填充。
        </td>
       </tr>

       <tr>
        <td><code class="literal">&#039;</code>(char)</td>
        <td>
         用字符（char）填充结果。
        </td>
       </tr>

      </tbody>
     
    </table>

   </p>
  </p>

  <p class="formalpara">
   <h5 class="title">Width</h5>
   <p class="para">
    要么是整数，表示转换结果应该有多少个字符（最少），要么是 <code class="literal">*</code>。如果使用
    <code class="literal">*</code>，那么宽度将作为额外的整数值提供，位于格式化符号之前。
   </p>
  </p>

  <p class="formalpara">
   <h5 class="title">Precision</h5>
   <p class="para">
    小数点 <code class="literal">.</code>，可选的后跟整数或者 <code class="literal">*</code>，其含义取决于格式化符号：
    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       <code class="literal">e</code>、<code class="literal">E</code>、<code class="literal">f</code>、<code class="literal">F</code>
       标志符：小数点后需要打印的位数（默认是 6）。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <code class="literal">g</code>、<code class="literal">G</code>、<code class="literal">h</code>、<code class="literal">H</code>
       标志符：这是要打印的最大有效位数。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <code class="literal">s</code> 标志符：充当分界点，为字符串设置最大字符限制。
      </span>
     </li>
    </ul>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <span class="simpara">
      如果小数点没有明确的精度值，则假设是 0。如果使用 <code class="literal">*</code>，则精度将作为额外的整数值提供，位于格式化符号之前。
     </span>
    </p></blockquote>
   </p>
  </p>

  <p class="para">
   <table class="doctable table">
    <caption><strong>标志符</strong></caption>
    
     <thead>
      <tr>
       <th>标志符</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><code class="literal">%</code></td>
       <td>
        字面意思的百分号字符。不需要参数。
       </td>
      </tr>

      <tr>
       <td><code class="literal">b</code></td>
       <td>
        参数视为整数并以二进制数字呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">c</code></td>
       <td>
        参数视为整数并以 ASCII 字符呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">d</code></td>
       <td>
        参数视为整数并以（有符号）十进制数字呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">e</code></td>
       <td>
        参数当做科学符号处理（例如 1.2e+2）。
       </td>
      </tr>

      <tr>
       <td><code class="literal">E</code></td>
       <td>
        与 <code class="literal">e</code> 标志符相同，但使用大写字母（例如 1.2E+2）。
       </td>
      </tr>

      <tr>
       <td><code class="literal">f</code></td>
       <td>
        参数当做浮点数处理且作为浮点数呈现（locale aware）。
       </td>
      </tr>

      <tr>
       <td><code class="literal">F</code></td>
       <td>
        参数当做浮点数处理且作为浮点数呈现（non-locale aware）。
       </td>
      </tr>

      <tr>
       <td><code class="literal">g</code></td>
       <td>
        <p class="para">
         通用格式。
        </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>
        参数视为整数并以八进制数字来呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">s</code></td>
       <td>
        参数视为字符串来呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">u</code></td>
       <td>
        参数视为整数并以无符号十进制数字呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">x</code></td>
       <td>
        参数视为整数并作为十六进制数字呈现（带小写字母）。
       </td>
      </tr>

      <tr>
       <td><code class="literal">X</code></td>
       <td>
        参数视为整数并作为十六进制数字呈现（带大写字母）。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>

  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    <code class="literal">c</code> 类型标志符忽略填充和宽度。
   </p>
  </div>

  <div class="warning"><strong class="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">
   变量将会强制转换为适合标志符的类型：
   <table class="doctable table">
    <caption><strong>类型处理</strong></caption>
    
     <thead>
      <tr>
       <th>类型</th>
       <th>标志符</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.vfprintf-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回输出字符串的长度。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.vfprintf-errors">
  <h3 class="title">错误／异常</h3>
  
  <p class="para">
   从 PHP 8.0.0 开始，如果参数个数为零，将抛出 <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>。在 PHP 8.0.0 之前，会发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>。
  </p>
  <p class="para">
   从 PHP 8.0.0 开始，如果 <code class="literal">[width]</code> 小于零或大于 <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>，则会抛出
   <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>。在 PHP 8.0.0 之前，会发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>。
  </p>
  <p class="para">
   从 PHP 8.0.0 开始，如果 <code class="literal">[precision]</code> 小于零或大于 <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>，则会抛出
   <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>。在 PHP 8.0.0 之前，会发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>。
  </p>
  <p class="para">
   从 PHP 8.0.0 开始，当传递的参数少于所需的参数时会抛出 <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>。在 PHP 8.0.0 之前，返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> 并发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>。
  </p>

 </div>


 <div class="refsect1 changelog" id="refsect1-function.vfprintf-changelog">
  <h3 class="title">更新日志</h3>
  
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       此函数失败时不再返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       如果参数个数为零则抛出 <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>；以前该函数则会发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>。
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       如果 <code class="literal">[width]</code> 小于零或大于 <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>，则抛出 <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>；以前该函数则会发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>。
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       如果 <code class="literal">[precision]</code> 小于零或大于 <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>，则抛出 <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>；以前该函数则会发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>。
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       当传递的参数少于所需的参数时抛出 <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>；以前该函数则会发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>。
      </td>
     </tr>

    </tbody>
   
  </table>


 </div>


 <div class="refsect1 examples" id="refsect1-function.vfprintf-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>vfprintf()</strong></span>: 前导 0 的整数</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">if (!(</span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'date.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'w'</span><span style="color: #007700">)))<br />    return;<br /><br /></span><span style="color: #0000BB">$year </span><span style="color: #007700">= </span><span style="color: #0000BB">2025</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 /></span><span style="color: #0000BB">vfprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"%04d-%02d-%02d"</span><span style="color: #007700">, array(</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 /></span><span style="color: #FF8000">// 将向 date.txt 写入格式化的 ISO 标准日期<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.vfprintf-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.printf.php" class="function" rel="rdfs-seeAlso">printf()</a> - 输出格式化字符串</span></li>
    <li><span class="function"><a href="function.sprintf.php" class="function" rel="rdfs-seeAlso">sprintf()</a> - 返回格式化字符串</span></li>
    <li><span class="function"><a href="function.fprintf.php" class="function" rel="rdfs-seeAlso">fprintf()</a> - 将格式化后的字符串写入到流</span></li>
    <li><span class="function"><a href="function.vprintf.php" class="function" rel="rdfs-seeAlso">vprintf()</a> - 输出格式化字符串</span></li>
    <li><span class="function"><a href="function.vsprintf.php" class="function" rel="rdfs-seeAlso">vsprintf()</a> - 返回格式化字符串</span></li>
    <li><span class="function"><a href="function.sscanf.php" class="function" rel="rdfs-seeAlso">sscanf()</a> - 根据指定格式解析输入的字符</span></li>
    <li><span class="function"><a href="function.fscanf.php" class="function" rel="rdfs-seeAlso">fscanf()</a> - 从文件中格式化输入</span></li>
    <li><span class="function"><a href="function.number-format.php" class="function" rel="rdfs-seeAlso">number_format()</a> - 以千位分隔符方式格式化一个数字</span></li>
    <li><span class="function"><a href="function.date.php" class="function" rel="rdfs-seeAlso">date()</a> - 格式化 Unix 时间戳</span></li>
   </ul>
  </p>
 </div>


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