Jump to content

use of return statement


dvayne

Recommended Posts

whats the difference between this

<?php 
function addition ($a, $b) 
{ 
$sum=$a+$b; 
echo "the sum is" .$sum; 
} 
?>

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<?php 
addition (8,2); 
?>
</body>
</html>

 

and this

 

<?php 
function addition ($a, $b) 
{ 
$sum=$a+$b; 
echo "the sum is" .$sum; 
return;
} 
?>

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<?php 
addition (8,2); 
?>
</body>
</html>

 

and this

<?php 
function addition ($a, $b) 
{ 
$sum=$a+$b; 
echo "the sum is" .$sum; 
return $sum;
} 
?>

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<?php 
addition (8,2); 
?>
</body>
</html>

 

please help me..

Link to comment
https://forums.phpfreaks.com/topic/86129-use-of-return-statement/
Share on other sites

you can use functions to return data. If your function returns the result of a math problem (like your last example), you can assign that returned value to a variable:

 

<?php 
function addition ($a, $b) 
{ 
$sum=$a+$b; 
echo "the sum is" .$sum; 
return $sum;
} 

$myReturnedVariable = addition(5,6); //$myReturnedVariable will = 11
?>

 

The first example echo's "the sum is whatever" and does not return anything.

I THINK the 2nd one is just wrong. (can anyone confirm that?)

The third example does what I showed you above.

 

Hope this helps!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.