Sophie

Sophie

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

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

<HTML
><HEAD
><TITLE
>Functions</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="Language Reference"
HREF="langref.html"><LINK
REL="PREVIOUS"
TITLE="include_once"
HREF="function.include-once.html"><LINK
REL="NEXT"
TITLE="Function arguments"
HREF="functions.arguments.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="chapter"
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="function.include-once.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="functions.arguments.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="chapter"
><H1
><A
NAME="functions"
>Chapter 13. Functions</A
></H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="functions.html#functions.user-defined"
>User-defined functions</A
></DT
><DT
><A
HREF="functions.arguments.html"
>Function arguments</A
></DT
><DT
><A
HREF="functions.returning-values.html"
>Returning values</A
></DT
><DT
><A
HREF="functions.old-syntax.html"
><TT
CLASS="literal"
>old_function</TT
></A
></DT
><DT
><A
HREF="functions.variable-functions.html"
>Variable functions</A
></DT
></DL
></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="functions.user-defined"
></A
>User-defined functions</H1
><P
>&#13;    A function may be defined using syntax such as the following:
 
    <DIV
CLASS="informalexample"
><A
NAME="AEN5313"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>function foo ($arg_1, $arg_2, ..., $arg_n)
{
    echo "Example function.\n";
    return $retval;
}</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    Any valid PHP code may appear inside a function, even other
    functions and <A
HREF="language.oop.html#keyword.class"
>class</A
>
    definitions.
   </P
><P
>&#13;    In PHP 3, functions must be defined before they are referenced. No
    such requirement exists in PHP 4. <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>Except</I
></SPAN
> when
    a function is conditionally defined such as shown in the two examples
    below.
   </P
><P
>&#13;    When a function is defined in a conditional manner such as the two
    examples shown.  Its definition must be processed <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>prior</I
></SPAN
>
    to being called.
    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5321"
></A
><P
><B
>Example 13-1. Conditional functions</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php

$makefoo = true;

/* We can't call foo() from here 
   since it doesn't exist yet,
   but we can call bar() */

bar();

if ($makefoo) {
  function foo ()
  {
    echo "I don't exist until program execution reaches me.\n";
  }
}

/* Now we can safely call foo()
   since $makefoo evaluated to true */

if ($makefoo) foo();

function bar() {
{
  echo "I exist immediately upon program start.\n";
}

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5324"
></A
><P
><B
>Example 13-2. Functions within functions</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
function foo() 
{
  function bar() 
  {
    echo "I don't exist until foo() is called.\n";
  }
}

/* We can't call bar() yet
   since it doesn't exist. */

foo();

/* Now we can call bar(),
   foo()'s processesing has
   made it accessable. */

bar();

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
><P
>&#13;    PHP does not support function overloading, nor is it possible to
    undefine or redefine previously-declared functions.
   </P
><P
>&#13;    PHP 3 does not support variable numbers of arguments to functions,
    although default arguments are supported (see <A
HREF="functions.arguments.html#functions.arguments.default"
>Default argument
    values</A
> for more information). PHP 4 supports both: 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.
   </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="function.include-once.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.arguments.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.include-once.html"
><B
CLASS="function"
>include_once()</B
></A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="langref.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Function arguments</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>