⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.sprintf.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Return a formatted string</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.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">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">string</span> <span class="methodname"><b><b>sprintf</b></b></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">   Returns a string produced according to the formatting string   <i><tt class="parameter">format</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">format</tt></i></span>     <dd>      <p class="para">       The format string is composed of zero or more directives:       ordinary characters (excluding <i>%</i>) 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 <b>sprintf()</b>       and <a href="function.printf.html" class="function">printf()</a>.      </p>      <p class="para">       Each conversion specification consists of a percent sign       (<i>%</i>), followed by one or more of these       elements, in order:       <ol class="orderedlist">        <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          <i>0</i> (zero character).  The default is to pad          with spaces.  An alternate padding character can be specified          by prefixing it with a single quote (<i>&#039;</i>).          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 <i>-</i>          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> 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">            <i>%</i> - a literal percent character. No            argument is required.           </li>           <li class="member">            <i>b</i> - the argument is treated as an            integer, and presented as a binary number.           </li>           <li class="member">            <i>c</i> - the argument is treated as an            integer, and presented as the character with that ASCII            value.           </li>           <li class="member">            <i>d</i> - the argument is treated as an            integer, and presented as a (signed) decimal number.           </li>           <li class="member">            <i>e</i> - 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">            <i>u</i> - the argument is treated as an            integer, and presented as an unsigned decimal number.           </li>           <li class="member">            <i>f</i> - the argument is treated as a            float, and presented as a floating-point number (locale aware).           </li>           <li class="member">            <i>F</i> - 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">            <i>o</i> - the argument is treated as an            integer, and presented as an octal number.           </li>           <li class="member">            <i>s</i> - the argument is treated as and            presented as a string.           </li>           <li class="member">            <i>x</i> - the argument is treated as an integer            and presented as a hexadecimal number (with lowercase            letters).           </li>           <li class="member">            <i>X</i> - 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">       The format string supports argument numbering/swapping.  Here is an       example:       <div class="example">        <p><b>Example #1 Argument swapping</b></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">'There&nbsp;are&nbsp;%d&nbsp;monkeys&nbsp;in&nbsp;the&nbsp;%s'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">printf</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 might 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">        <p><b>Example #2 Argument swapping</b></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 /></span><span style="color: #0000BB">printf</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">        <p><b>Example #3 Argument swapping</b></p>        <div class="example-contents">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -