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!

Dani AI

Generated

Short answer: return stops execution and optionally hands a value back to the caller. In functions it ends the function and gives the caller a value to use later; in PHP an included file can also return a value to the includer.

As explained, use a plain-output function when only a side effect is needed (echo / console.log). Use return when the result must be consumed later. Example (JavaScript) showing the difference:

function showDouble(n) {
  console.log(n * 2); // side effect only
}

function double(n) {
  return n * 2;       // value returned for further use
}

let x = double(4); // x === 8
showDouble(4);     // nothing to assign

As mentioned, PHP include/require can return a value from the included file. A common pattern is a config file that returns an array:

# config.php
<?php
return [
  'host' => 'localhost',
  'dbname' => 'app',
];

# main.php
$config = include 'config.php';
echo $config['host'];

Practical gotchas and tips:

  • return with no expression yields undefined in JS and NULL in PHP.
  • include returns the included file's return value; if the file has no return it yields 1 on success and false on failure (check warnings). See PHP docs: include and .
  • Use early return to reduce nesting and improve readability.
  • In JS, return from a callback does not return from the outer function; use Promises/async-await for async flows. See MDN: return statement.

Recommended Answers

All 2 Replies

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.