<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="Documentaþia limbajului" 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-2"></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" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.include-once.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="functions.arguments.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.functions" >Cap. 12. Functions</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Cuprins</B ></DT ><DT ><A HREF="language.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.variable-functions.html" >Variable functions</A ></DT ><DT ><A HREF="functions.internal.html" >Internal (built-in) functions</A ></DT ></DL ></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="functions.user-defined" >User-defined functions</A ></H1 ><P > A function may be defined using syntax such as the following: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2968" ></A ><P ><B >Exemplu 12-1. Pseudo code to demonstrate function uses</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">foo</font><font color="#007700">(</font><font color="#0000BB">$arg_1</font><font color="#007700">, </font><font color="#0000BB">$arg_2</font><font color="#007700">, </font><font color="#FF8000">/* ..., */ </font><font color="#0000BB">$arg_n</font><font color="#007700">)<br />{<br /> echo </font><font color="#DD0000">"Example function.\n"</font><font color="#007700">;<br /> return </font><font color="#0000BB">$retval</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 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 > 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 > 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. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2978" ></A ><P ><B >Exemplu 12-2. Conditional functions</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br />$makefoo </font><font color="#007700">= </font><font color="#0000BB">true</font><font color="#007700">;<br /><br /></font><font color="#FF8000">/* We can't call foo() from here <br /> since it doesn't exist yet,<br /> but we can call bar() */<br /><br /></font><font color="#0000BB">bar</font><font color="#007700">();<br /><br />if (</font><font color="#0000BB">$makefoo</font><font color="#007700">) {<br /> function </font><font color="#0000BB">foo</font><font color="#007700">()<br /> {<br /> echo </font><font color="#DD0000">"I don't exist until program execution reaches me.\n"</font><font color="#007700">;<br /> }<br />}<br /><br /></font><font color="#FF8000">/* Now we can safely call foo()<br /> since $makefoo evaluated to true */<br /><br /></font><font color="#007700">if (</font><font color="#0000BB">$makefoo</font><font color="#007700">) </font><font color="#0000BB">foo</font><font color="#007700">();<br /><br />function </font><font color="#0000BB">bar</font><font color="#007700">() <br />{<br /> echo </font><font color="#DD0000">"I exist immediately upon program start.\n"</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2982" ></A ><P ><B >Exemplu 12-3. Functions within functions</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">foo</font><font color="#007700">() <br />{<br /> function </font><font color="#0000BB">bar</font><font color="#007700">() <br /> {<br /> echo </font><font color="#DD0000">"I don't exist until foo() is called.\n"</font><font color="#007700">;<br /> }<br />}<br /><br /></font><font color="#FF8000">/* We can't call bar() yet<br /> since it doesn't exist. */<br /><br /></font><font color="#0000BB">foo</font><font color="#007700">();<br /><br /></font><font color="#FF8000">/* Now we can call bar(),<br /> foo()'s processesing has<br /> made it accessible. */<br /><br /></font><font color="#0000BB">bar</font><font color="#007700">();<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration. </P ></BLOCKQUOTE ></DIV ><P > 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" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="functions.arguments.html" ACCESSKEY="N" >Înainte</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" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Function arguments</TD ></TR ></TABLE ></DIV ></BODY ></HTML >