Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 2934

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>Function arguments</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="Functions"
HREF="functions.html"><LINK
REL="PREVIOUS"
TITLE="Functions"
HREF="functions.html"><LINK
REL="NEXT"
TITLE="Returning values"
HREF="functions.returning-values.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="functions.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 13. Functions</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="functions.returning-values.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="functions.arguments"
></A
>Function arguments</H1
><P
>&#13;    Information may be passed to functions via the argument list,
    which is a comma-delimited list of variables and/or constants.
   </P
><P
>&#13;     PHP supports passing arguments by value (the default), <A
HREF="functions.arguments.html#functions.arguments.by-reference"
>passing by
     reference</A
>, and <A
HREF="functions.arguments.html#functions.arguments.default"
>default argument
     values</A
>. Variable-length argument lists are supported only
     in PHP 4 and later; see <A
HREF="functions.arguments.html#functions.variable-arg-list"
>Variable-length argument
     lists</A
> and the function references for
     <A
HREF="function.func-num-args.html"
><B
CLASS="function"
>func_num_args()</B
></A
>,
     <A
HREF="function.func-get-arg.html"
><B
CLASS="function"
>func_get_arg()</B
></A
>, and
     <A
HREF="function.func-get-args.html"
><B
CLASS="function"
>func_get_args()</B
></A
> for more information. A
     similar effect can be achieved in PHP 3 by passing an array of
     arguments to a function:
 
    <DIV
CLASS="informalexample"
><A
NAME="AEN5344"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>function takes_array($input)
{
    echo "$input[0] + $input[1] = ", $input[0]+$input[1];
}</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="functions.arguments.by-reference"
></A
>Making arguments be passed by reference</H2
><P
>&#13;     By default, function arguments are passed by value (so that if
     you change the value of the argument within the function, it does
     not get changed outside of the function). If you wish to allow a
     function to modify its arguments, you must pass them by
     reference.
    </P
><P
>&#13;     If you want an argument to a function to always be passed by
     reference, you can prepend an ampersand (&#38;) to the argument
     name in the function definition:
 
     <DIV
CLASS="informalexample"
><A
NAME="AEN5350"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>function add_some_extra(&#38;$string)
{
    $string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str;    // outputs 'This is a string, and something extra.'</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="functions.arguments.default"
></A
>Default argument values</H2
><P
>&#13;     A function may define C++-style default values for scalar
     arguments as follows:
 
     <DIV
CLASS="informalexample"
><A
NAME="AEN5355"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>function makecoffee ($type = "cappuccino")
{
    return "Making a cup of $type.\n";
}
echo makecoffee ();
echo makecoffee ("espresso");</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     The output from the above snippet is:
 
     <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Making a cup of cappuccino.
Making a cup of espresso.</PRE
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     The default value must be a constant expression, not (for
     example) a variable or class member.
    </P
><P
>&#13;     Note that when using default arguments, any defaults should be on
     the right side of any non-default arguments; otherwise, things
     will not work as expected. Consider the following code snippet:
 
     <DIV
CLASS="informalexample"
><A
NAME="AEN5361"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>function makeyogurt ($type = "acidophilus", $flavour)
{
    return "Making a bowl of $type $flavour.\n";
}
 
echo makeyogurt ("raspberry");   // won't work as expected</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     The output of the above example is:
 
     <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Warning: Missing argument 2 in call to makeyogurt() in 
/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
Making a bowl of raspberry .</PRE
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     Now, compare the above with this:
 
     <DIV
CLASS="informalexample"
><A
NAME="AEN5366"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>function makeyogurt ($flavour, $type = "acidophilus")
{
    return "Making a bowl of $type $flavour.\n";
}
 
echo makeyogurt ("raspberry");   // works as expected</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     The output of this example is:
 
     <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Making a bowl of acidophilus raspberry.</PRE
></TD
></TR
></TABLE
>
    </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="functions.variable-arg-list"
></A
>Variable-length argument lists</H2
><P
>&#13;     PHP 4 has support for variable-length argument lists in
     user-defined functions. This is really quite easy, using the
     <A
HREF="function.func-num-args.html"
><B
CLASS="function"
>func_num_args()</B
></A
>,
     <A
HREF="function.func-get-arg.html"
><B
CLASS="function"
>func_get_arg()</B
></A
>, and
     <A
HREF="function.func-get-args.html"
><B
CLASS="function"
>func_get_args()</B
></A
> functions.
    </P
><P
>&#13;     No special syntax is required, and argument lists may still be
     explicitly provided with function definitions and will behave as
     normal.
    </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="functions.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="functions.returning-values.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Functions</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="functions.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Returning values</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>