<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Returning values</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="Functions" HREF="language.functions.html"><LINK REL="PREVIOUS" TITLE="Function arguments" HREF="functions.arguments.html"><LINK REL="NEXT" TITLE="Variable functions" HREF="functions.variable-functions.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></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" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="functions.arguments.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Cap. 12. Functions</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="functions.variable-functions.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="functions.returning-values" >Returning values</A ></H1 ><P > Values are returned by using the optional return statement. Any type may be returned, including lists and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called. See <A HREF="function.return.html" ><B CLASS="function" >return()</B ></A > for more information. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3060" ></A ><P ><B >Exemplu 12-10. Use of <A HREF="function.return.html" ><B CLASS="function" >return()</B ></A ></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">square</font><font color="#007700">(</font><font color="#0000BB">$num</font><font color="#007700">)<br />{<br /> return </font><font color="#0000BB">$num </font><font color="#007700">* </font><font color="#0000BB">$num</font><font color="#007700">;<br />}<br />echo </font><font color="#0000BB">square</font><font color="#007700">(</font><font color="#0000BB">4</font><font color="#007700">); </font><font color="#FF8000">// outputs '16'.<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > You can't return multiple values from a function, but similar results can be obtained by returning a list. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3066" ></A ><P ><B >Exemplu 12-11. Returning an array to get multiple values</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">small_numbers</font><font color="#007700">()<br />{<br /> return array (</font><font color="#0000BB">0</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">);<br />}<br />list (</font><font color="#0000BB">$zero</font><font color="#007700">, </font><font color="#0000BB">$one</font><font color="#007700">, </font><font color="#0000BB">$two</font><font color="#007700">) = </font><font color="#0000BB">small_numbers</font><font color="#007700">();<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > To return a reference from a function, you have to use the reference operator & in both the function declaration and when assigning the returned value to a variable: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3071" ></A ><P ><B >Exemplu 12-12. Returning a reference from a function</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">returns_reference</font><font color="#007700">()<br />{<br /> return </font><font color="#0000BB">$someref</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">$newref </font><font color="#007700">=& </font><font color="#0000BB">returns_reference</font><font color="#007700">();<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > For more information on references, please check out <A HREF="language.references.html" >References Explained</A >. </P ></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.arguments.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.variable-functions.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Function arguments</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.functions.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Variable functions</TD ></TR ></TABLE ></DIV ></BODY ></HTML >