It's useful if you want a function to return a value.
i.e.
Function FavouritePie($who) {
switch($who) {
case 'John':
return 'apple';
case 'Peter':
return 'Rhubarb';
}
}
So, considering the following:
$WhatPie = FavouritePie('John');
echo $WhatPie;
would give
apple
In a really simple form, it's useful if you want a function to return something, i.e. process it and pass something back. Without a return, you'd just be performing a function with a dead end.
Some further reading:
http://php.net/manual/en/function.return.php
http://php.net/manual/en/functions.returning-values.php
To add some further context specific to the answer, if the question boils down to "Do I need to add a return
for the sake of it, at the end of a function, then the answer is no. But that doesn't mean the correct answer is always to leave out your return
.
it depends what you want to do with $a
.
If you echo $a
within the function, that function will spit out $a
as soon as it is called. If you return $a
, assuming you set the calling of test
to a variable (i.e. $something = $test('foo')
), then you can use it later on.