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!

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.