0

this is a noob question. I'm quite confused sometimes when to use 'return' everytime Im doing my php and javascript codes. Maybe a plain english answer can help me : ) thanks!

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by vibhaJ
0

The "return" statement is used primarily in two areas. One is custom functions that you define and the other is in the include() and require() to return from the required or included page a variable. So here's a sample script.

<?php //index.php
function myfile() {
return 'test.php';
}

$variable=include(myfile());
//$variable now has the value 'test'
<?php //test.php
return 'test';
?>

As you can see in that script is all of the uses for return. Basically it ends the subscript and returns to the parent script.

0

Normally there are two used of functions.
1) Just call function abstractly, and you don't need anything from function.
e.g.

function echoSum($a,$b)
{
	$c = ($a+$b);
	echo "Sum is :".$c;
}
//==== some php code =====
echoSum($a,$b) // function output is not required further
//==== some php code =====

Here you are just adding both values and echo it in function.
in your further code you are not bothered about function result.

2) Call function and you want to use outcome of function in further code.
e.g.

function getSum($a,$b)
{
	$c = ($a+$b);
	return $c;
}
//==== some php code =====
$sum = getSum($a,$b);
$sum10 = $sum + 10; // use function output further
echo "Sum is :".$sum ." & Sum10 is :".$sum10;
//==== some php code =====

Here you are again adding two values but in further code you want to use outcome of function.
Your next code will depend on function result or output.

Thus you can use return in second case.

Edited by vibhaJ: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.