📄 function.fprintf.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Write a formatted string to a stream</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.explode.html">explode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.get-html-translation-table.html">get_html_translation_table</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.fprintf" class="refentry"> <div class="refnamediv"> <h1 class="refname">fprintf</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">fprintf</span> — <span class="dc-title">Write a formatted string to a stream</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>fprintf</b></b></span> ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$handle</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$format</tt></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$args</tt></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$...</tt></span> ]] )</div> <p class="para rdfs-comment"> Write a string produced according to <i><tt class="parameter">format</tt></i> to the stream resource specified by <i><tt class="parameter">handle</tt></i>. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">handle</tt></i></span> <dd> <p class="para">A file system pointer <a href="language.types.resource.html" class="type resource">resource</a>that is typically created using <a href="function.fopen.html" class="function">fopen()</a>.</p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">format</tt></i></span> <dd> <p class="para"> See <a href="function.sprintf.html" class="function">sprintf()</a> for a description of <i><tt class="parameter">format</tt></i>. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">args</tt></i></span> <dd> <p class="para"> </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">...</tt></i></span> <dd> <p class="para"> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the length of the string written. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>fprintf()</b>: zero-padded integers</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?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 /><br /></span><span style="color: #0000BB">fprintf</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">, </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">// will write the formatted ISO date to date.txt<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="example"> <p><b>Example #2 <b>fprintf()</b>: formatting currency</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?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">'currency.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'w'</span><span style="color: #007700">))) {<br /> return;<br />}<br /><br /></span><span style="color: #0000BB">$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 /></span><span style="color: #FF8000">// echo $money will output "123.1";<br /></span><span style="color: #0000BB">$len </span><span style="color: #007700">= </span><span style="color: #0000BB">fprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</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 /></span><span style="color: #FF8000">// will write "123.10" to currency.txt<br /><br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"wrote $len bytes to currency.txt"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// use the return value of fprintf to determine how many bytes we wrote<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a></li> <li class="member"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a></li> <li class="member"><a href="function.sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a></li> <li class="member"><a href="function.fscanf.html" class="function" rel="rdfs-seeAlso">fscanf()</a></li> <li class="member"><a href="function.vsprintf.html" class="function" rel="rdfs-seeAlso">vsprintf()</a></li> <li class="member"><a href="function.number-format.html" class="function" rel="rdfs-seeAlso">number_format()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.explode.html">explode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.get-html-translation-table.html">get_html_translation_table</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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -