Beispiel #1
0
function add_subt($val1, $val2)
{
    $add = $val1 + $val2;
    $subt = $val1 - $val2;
    return array($add, $subt);
    //one return per function...
    //but returning multiple values
    list($add_result, $subt_result) = add_subt(20, 7);
    echo "Add: " . $add_result . "<br />";
    // NO array keys required
    echo "Subt: " . $subt_result . "<br />";
    // NO array keys required
}
Beispiel #2
0
<head lang = "en">
	<meta charset = "UTF-8">
	<title>multi returns</title>
</head>
<body>

<?php 
function add_subt($val1, $val2)
{
    $add = $val1 + $val2;
    $subt = $val1 - $val2;
    return array($add, $subt);
    //one return per function
    //however, you can return multiple values
}
$result_array = add_subt(10, 5);
//Line #13 adds 10 + 5; Gets returned as 1st item in array
echo "Add: " . $result_array[0] . "<br />";
//Echo the array key value
echo "Subt: " . $result_array[1] . "<br />";
//Line #14 subtracts 10 - 5; Gets returned as 2nd item in array
?>

<br /><br />
use list to assign "meaningful" array variables
	<?php 
function add_subt1($val3, $val4)
{
    $add1 = $val3 + $val4;
    $subt1 = $val3 - $val4;
    return array($add1, $subt1);
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
	<head>
		<title>Functions: Multiple Returns</title>
	</head>
	<body>
		
		<?php 
function add_subt($val1, $val2)
{
    $add = $val1 + $val2;
    $subt = $val1 - $val2;
    return array($add, $subt);
}
$result_array = add_subt(10, 5);
echo "Add: " . $result_array[0] . "<br />";
echo "Subt: " . $result_array[1] . "<br />";
list($add_result, $subt_result) = add_subt(20, 7);
echo "Add: " . $add_result . "<br />";
echo "Subt: " . $subt_result . "<br />";
?>
		
	</body>
</html>
	<head> 
		<title>Functions: Multiple Returns</title>
	</head>
	
	<body> 

		<?php 
// creating a function w/ multiple returns
function add_subt($val1, $val2)
{
    $add = $val1 + $val2;
    $subt = $val1 - $val2;
    return [$add, $subt];
}
?>
		
		<?php 
$result_array = add_subt(10, 5);
echo "Add: {$result_array[0]} <br /> Subtract: {$result_array[1]}";
?>
		
		<?php 
list($add_result, $subt_result) = add_subt(10, 10);
echo "Add: {$add_result} <br /> Subtract: {$subt_result}";
?>
			
		
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>	
	<script src=""></script> 
	</body>
</html>