Search DaTutorials:  
HOME
Php Tutorials
Articles
Basics
Date and Time
File Manipulations
Forms
Functions
General
Interactivity
MySql
Navigation
News,Shoutboxes and Blogs
Language Structure and Data Types
Searching
Security
String Manipulation
User Information and Stats
Search For a Tutorial
Php Reference

Returning values

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 return() for more information.

Example 12-10. Use of return()

<?php
function square($num)
{
   return
$num * $num;
}
echo
square(4);  // outputs '16'.
?>

You can't return multiple values from a function, but similar results can be obtained by returning a list.

Example 12-11. Returning an array to get multiple values

<?php
function small_numbers()
{
   return array (
0, 1, 2);
}
list (
$zero, $one, $two) = small_numbers();
?>

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:

Example 12-12. Returning a reference from a function

<?php
function &returns_reference()
{
   return
$someref;
}

$newref =& returns_reference();
?>

For more information on references, please check out References Explained.



Copyright © 2001-2004 The PHP Group
All rights reserved.

     Mortgages | Flights | Mobile Phones | Mortgage Calculator | Bad Credit Loan
Home     Link To Us     Ad With Us     Contact Us     Tell A Friend     Affiliates     Blog     MsOfficeHelp