Home Tutorial Php Phpbasics Tutorial PHP Returning Values from Function

 
 

PHP Returning Values from Function
Posted on: March 29, 2010 at 12:00 AM
Advertisement
In this tutorial we will study about returning values from functions in PHP, how to return values from a function, what kind of data types are supported by the return statement are discussed in this tutorial. Examples in this tutorial will make it more clear.

Returnig Values from function:

From a function we can return values using optional return statement, in PHP we can return arrays, objects. With the help of the return statement the control moves back to the calling portion of the project. If we do not put any return statement then a NULL value is returned. A function can not have more than one return statement because whenever a return statement occurs rest of the statements are ignored by the parser.  To know more about return statement please visit our site:

www.roseindia.net/tutorial/php/phpbasics/tutorial/PHP-Return.html

Example:

<?php

function square($var){

return $var*$var;

}

echo "Square of 2 is: ".square(2);

?>

Output:

Square of 2 is: 4

Example:

<?php

function fun(){

return array(0,1,2);

}

list($var1,$var2,$var3)=fun();

echo "Value of \$var1= ".$var1."<br/>";

echo "Value of \$var2= ".$var2."<br/>";

echo "Value of \$var3= ".$var3."<br/>";

?>

Output:

Value of $var1= 0
Value of $var2= 1
Value of $var3= 2

 

 

Advertisement

Related Tags for PHP Returning Values from Function:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 29, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com