<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Returns an array comprising a function's argument list</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.func-get-arg.html">func_get_arg</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.func-num-args.html">func_num_args</a></div> <div class="up"><a href="ref.funchand.html">Function handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.func-get-args" class="refentry"> <div class="refnamediv"> <h1 class="refname">func_get_args</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">func_get_args</span> — <span class="dc-title">Returns an array comprising a function's argument list</span></p> </div> <div class="refsect1 description" id="refsect1-function.func-get-args-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>func_get_args</strong></span> ( <span class="methodparam">void</span> )</div> <p class="para rdfs-comment"> Gets an array of the function's argument list. </p> <p class="para"> This function may be used in conjunction with <span class="function"><a href="function.func-get-arg.html" class="function">func_get_arg()</a></span> and <span class="function"><a href="function.func-num-args.html" class="function">func_num_args()</a></span> to allow user-defined functions to accept variable-length argument lists. </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.func-get-args-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an array in which each element is a copy of the corresponding member of the current user-defined function's argument list. </p> </div> <div class="refsect1 changelog" id="refsect1-function.func-get-args-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.0</td> <td> This function can now be used in parameter lists. </td> </tr> <tr> <td>5.3.0</td> <td> If this function is called from the outermost scope of a file which has been included by calling <span class="function"><a href="function.include.html" class="function">include</a></span> or <span class="function"><a href="function.require.html" class="function">require</a></span> from within a function in the calling file, it now generates a warning and returns <strong><code>FALSE</code></strong>. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 errors" id="refsect1-function.func-get-args-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> Generates a warning if called from outside of a user-defined function. </p> </div> <div class="refsect1 examples" id="refsect1-function.func-get-args-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5111"> <p><strong>Example #1 <span class="function"><strong>func_get_args()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br /> </span><span style="color: #0000BB">$numargs </span><span style="color: #007700">= </span><span style="color: #0000BB">func_num_args</span><span style="color: #007700">();<br /> echo </span><span style="color: #DD0000">"Number of arguments: </span><span style="color: #0000BB">$numargs</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br /> if (</span><span style="color: #0000BB">$numargs </span><span style="color: #007700">>= </span><span style="color: #0000BB">2</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Second argument is: " </span><span style="color: #007700">. </span><span style="color: #0000BB">func_get_arg</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">) . </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">$arg_list </span><span style="color: #007700">= </span><span style="color: #0000BB">func_get_args</span><span style="color: #007700">();<br /> for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">< </span><span style="color: #0000BB">$numargs</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> echo </span><span style="color: #DD0000">"Argument </span><span style="color: #0000BB">$i</span><span style="color: #DD0000"> is: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$arg_list</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">] . </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Number of arguments: 3<br /> Second argument is: 2<br /> Argument 0 is: 1<br /> Argument 1 is: 2<br /> Argument 2 is: 3<br /> </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5112"> <p><strong>Example #2 <span class="function"><strong>func_get_args()</strong></span> example before and after PHP 5.3</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> test.php<br /><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">() {<br /> include </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 arg'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Second arg'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?><br /></span><br />fga.inc<br /><span style="color: #0000BB"><?php<br /><br />$args </span><span style="color: #007700">= </span><span style="color: #0000BB">func_get_args</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$args</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p> Output previous to PHP 5.3: </p></div> <div class="example-contents screen"> <div class="cdata"><pre> array ( 0 => 'First arg', 1 => 'Second arg', ) </pre></div> </div> <div class="example-contents"><p> Output in PHP 5.3 and later: </p></div> <div class="example-contents screen"> <div class="cdata"><pre> Warning: func_get_args(): Called from the global scope - no function context in /home/torben/Desktop/code/ml/fga.inc on line 3 false </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5113"> <p><strong>Example #3 <span class="function"><strong>func_get_args()</strong></span> example of byref and byval arguments</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">byVal</span><span style="color: #007700">(</span><span style="color: #0000BB">$arg</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'As passed : '</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">func_get_args</span><span style="color: #007700">()), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$arg </span><span style="color: #007700">= </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">'After change : '</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">func_get_args</span><span style="color: #007700">()), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br />function </span><span style="color: #0000BB">byRef</span><span style="color: #007700">(&</span><span style="color: #0000BB">$arg</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'As passed : '</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">func_get_args</span><span style="color: #007700">()), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$arg </span><span style="color: #007700">= </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">'After change : '</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">func_get_args</span><span style="color: #007700">()), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$arg </span><span style="color: #007700">= </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">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"><br /> As passed : array (<br /> 0 => 'bar',<br /> )<br /> After change : array (<br /> 0 => 'bar',<br /> )<br /> As passed : array (<br /> 0 => 'bar',<br /> )<br /> After change : array (<br /> 0 => 'baz',<br /> )<br /> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.func-get-args-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter in versions prior to 5.3.0. If this value must be passed, the results should be assigned to a variable, and that variable should be passed.</p></p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">If the arguments are passed by reference, any changes to the arguments will be reflected in the values returned by this function.</p></p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> This function returns a copy of the passed arguments only, and does not account for default (non-passed) arguments. </span> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.func-get-args-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.func-get-arg.html" class="function" rel="rdfs-seeAlso">func_get_arg()</a> - Return an item from the argument list</span></li> <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> <li class="member"> <span class="methodname"><a href="reflectionfunctionabstract.getparameters.html" class="methodname" rel="rdfs-seeAlso">ReflectionFunctionAbstract::getParameters()</a> - Gets parameters</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.func-get-arg.html">func_get_arg</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.func-num-args.html">func_num_args</a></div> <div class="up"><a href="ref.funchand.html">Function handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>