PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/php/function.func-get-arg.html

https://bitbucket.org/thncr/manuals
HTML | 218 lines | 180 code | 38 blank | 0 comment | 0 complexity | be0bb261d99da95dfaee6102183e5660 MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5. <title>Return an item from the argument list</title>
  6. </head>
  7. <body><div class="manualnavbar" style="text-align: center;">
  8. <div class="prev" style="text-align: left; float: left;"><a href="function.forward-static-call.html">forward_static_call</a></div>
  9. <div class="next" style="text-align: right; float: right;"><a href="function.func-get-args.html">func_get_args</a></div>
  10. <div class="up"><a href="ref.funchand.html">Function handling 函数</a></div>
  11. <div class="home"><a href="index.html">PHP Manual</a></div>
  12. </div><hr /><div id="function.func-get-arg" class="refentry">
  13. <div class="refnamediv">
  14. <h1 class="refname">func_get_arg</h1>
  15. <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">func_get_arg</span> &mdash; <span class="dc-title">Return an item from the argument list</span></p>
  16. </div>
  17. <div class="refsect1 description" id="refsect1-function.func-get-arg-description">
  18. <h3 class="title">说明</h3>
  19. <div class="methodsynopsis dc-description">
  20. <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>func_get_arg</strong></span>
  21. ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$arg_num</code></span>
  22. )</div>
  23. <p class="para rdfs-comment">
  24. Gets the specified argument from a user-defined function&#039;s argument list.
  25. </p>
  26. <p class="para">
  27. This function may be used in conjunction with
  28. <span class="function"><a href="function.func-get-args.html" class="function">func_get_args()</a></span> and <span class="function"><a href="function.func-num-args.html" class="function">func_num_args()</a></span>
  29. to allow user-defined functions to accept variable-length argument lists.
  30. </p>
  31. </div>
  32. <div class="refsect1 parameters" id="refsect1-function.func-get-arg-parameters">
  33. <h3 class="title">参数</h3>
  34. <p class="para">
  35. <dl>
  36. <dt>
  37. <span class="term"><em><code class="parameter">arg_num</code></em></span>
  38. <dd>
  39. <p class="para">
  40. The argument offset. Function arguments are counted starting from
  41. zero.
  42. </p>
  43. </dd>
  44. </dt>
  45. </dl>
  46. </p>
  47. </div>
  48. <div class="refsect1 returnvalues" id="refsect1-function.func-get-arg-returnvalues">
  49. <h3 class="title">返回值</h3>
  50. <p class="para">
  51. Returns the specified argument, or <strong><code>FALSE</code></strong> on error.
  52. </p>
  53. </div>
  54. <div class="refsect1 changelog" id="refsect1-function.func-get-arg-changelog">
  55. <h3 class="title">更新日志</h3>
  56. <p class="para">
  57. <table class="doctable informaltable">
  58. <thead>
  59. <tr>
  60. <th>版本</th>
  61. <th>说明</th>
  62. </tr>
  63. </thead>
  64. <tbody class="tbody">
  65. <tr>
  66. <td>5.3.0</td>
  67. <td>
  68. This function can now be used in parameter lists.
  69. </td>
  70. </tr>
  71. <tr>
  72. <td>5.3.0</td>
  73. <td>
  74. If this function is called from the outermost scope of a file
  75. which has been included by calling <span class="function"><a href="function.include.html" class="function">include</a></span>
  76. or <span class="function"><a href="function.require.html" class="function">require</a></span> from within a function in the
  77. calling file, it now generates a warning and returns <strong><code>FALSE</code></strong>.
  78. </td>
  79. </tr>
  80. </tbody>
  81. </table>
  82. </p>
  83. </div>
  84. <div class="refsect1 errors" id="refsect1-function.func-get-arg-errors">
  85. <h3 class="title">错误异常</h3>
  86. <p class="para">
  87. Generates a warning if called from outside of a user-defined function, or
  88. if <em><code class="parameter">arg_num</code></em> is greater than the number of arguments
  89. actually passed.
  90. </p>
  91. </div>
  92. <div class="refsect1 examples" id="refsect1-function.func-get-arg-examples">
  93. <h3 class="title">范例</h3>
  94. <p class="para">
  95. <div class="example" id="example-4866">
  96. <p><strong>Example #1 <span class="function"><strong>func_get_arg()</strong></span> example</strong></p>
  97. <div class="example-contents">
  98. <div class="phpcode"><code><span style="color: #000000">
  99. <span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$numargs&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">func_num_args</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Number&nbsp;of&nbsp;arguments:&nbsp;</span><span style="color: #0000BB">$numargs</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$numargs&nbsp;</span><span style="color: #007700">&gt;=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Second&nbsp;argument&nbsp;is:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">func_get_arg</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
  100. </span>
  101. </code></div>
  102. </div>
  103. </div>
  104. </p>
  105. <p class="para">
  106. <div class="example" id="example-4867">
  107. <p><strong>Example #2 <span class="function"><strong>func_get_arg()</strong></span> example before and
  108. after PHP 5.3</strong></p>
  109. <div class="example-contents">
  110. <div class="phpcode"><code><span style="color: #000000">
  111. test.php<br /><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;include&nbsp;</span><span style="color: #DD0000">'./fga.inc'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #DD0000">'First&nbsp;arg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Second&nbsp;arg'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;<br /></span><br />fga.php<br /><span style="color: #0000BB">&lt;?php<br /><br />$arg&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">func_get_arg</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$arg</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
  112. </span>
  113. </code></div>
  114. </div>
  115. <div class="example-contents"><p>
  116. Output previous to PHP 5.3:
  117. </p></div>
  118. <div class="example-contents screen">
  119. <div class="cdata"><pre>
  120. &#039;Second arg&#039;
  121. </pre></div>
  122. </div>
  123. <div class="example-contents"><p>
  124. Output in PHP 5.3 and later:
  125. </p></div>
  126. <div class="example-contents screen">
  127. <div class="cdata"><pre>
  128. Warning: func_get_arg(): Called from the global scope - no function
  129. context in /home/torben/Desktop/code/ml/fga.inc on line 3
  130. false
  131. </pre></div>
  132. </div>
  133. </div>
  134. </p>
  135. <p class="para">
  136. <div class="example" id="example-4868">
  137. <p><strong>Example #3 <span class="function"><strong>func_get_arg()</strong></span> example of byref and byval arguments</strong></p>
  138. <div class="example-contents">
  139. <div class="phpcode"><code><span style="color: #000000">
  140. <span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">byVal</span><span style="color: #007700">(</span><span style="color: #0000BB">$arg</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'As&nbsp;passed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">func_get_arg</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$arg&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'After&nbsp;change&nbsp;&nbsp;:&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">func_get_arg</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">byRef</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$arg</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'As&nbsp;passed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">func_get_arg</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$arg&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'After&nbsp;change&nbsp;&nbsp;:&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">func_get_arg</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$arg&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">byVal</span><span style="color: #007700">(</span><span style="color: #0000BB">$arg</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">byRef</span><span style="color: #007700">(</span><span style="color: #0000BB">$arg</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
  141. </span>
  142. </code></div>
  143. </div>
  144. <div class="example-contents"><p>以上例程会输出</p></div>
  145. <div class="example-contents screen"><br />
  146. As passed : &#039;bar&#039;<br />
  147. After change : &#039;bar&#039;<br />
  148. As passed : &#039;bar&#039;<br />
  149. After change : &#039;baz&#039;<br />
  150. </div>
  151. </div>
  152. </p>
  153. </div>
  154. <div class="refsect1 notes" id="refsect1-function.func-get-arg-notes">
  155. <h3 class="title">注释</h3>
  156. <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">因为函数依赖于当前作用域以确定参数的细节所以在
  157. 5.3.0 以前的版本中不能用作函数的参数如必须传递此值时可将结果赋与一个变量然后用此变量进行传递</p></p></blockquote>
  158. <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">如果参数以引用方式传递函数对该参数的任何改变将在函数返回后保留</p></p></blockquote>
  159. <blockquote class="note"><p><strong class="note">Note</strong>:
  160. <span class="simpara">
  161. This function returns a copy of the passed arguments only, and does not
  162. account for default (non-passed) arguments.
  163. </span>
  164. </p></blockquote>
  165. </div>
  166. <div class="refsect1 seealso" id="refsect1-function.func-get-arg-seealso">
  167. <h3 class="title">参见</h3>
  168. <p class="para">
  169. <ul class="simplelist">
  170. <li class="member"> <span class="function"><a href="function.func-get-args.html" class="function" rel="rdfs-seeAlso">func_get_args()</a> - Returns an array comprising a function's argument list</span></li>
  171. <li class="member"> <span class="function"><a href="function.func-num-args.html" class="function" rel="rdfs-seeAlso">func_num_args()</a> - Returns the number of arguments passed to the function</span></li>
  172. </ul>
  173. </p>
  174. </div>
  175. </div><hr /><div class="manualnavbar" style="text-align: center;">
  176. <div class="prev" style="text-align: left; float: left;"><a href="function.forward-static-call.html">forward_static_call</a></div>
  177. <div class="next" style="text-align: right; float: right;"><a href="function.func-get-args.html">func_get_args</a></div>
  178. <div class="up"><a href="ref.funchand.html">Function handling 函数</a></div>
  179. <div class="home"><a href="index.html">PHP Manual</a></div>
  180. </div></body></html>