I was wondering if a user defined function will always needs a return() statement and if not when is a return() statement not required?
7
-
php.net/manual/en/function.return.phpTJHeuvel– TJHeuvel2011-08-31 10:48:20 +00:00Commented Aug 31, 2011 at 10:48
-
what is with the negative rating?newSO– newSO2011-08-31 10:54:53 +00:00Commented Aug 31, 2011 at 10:54
-
Because its not really a question, commonly known and well documented.TJHeuvel– TJHeuvel2011-08-31 10:58:19 +00:00Commented Aug 31, 2011 at 10:58
-
Its still a valid question not every one knows everything about php or do they?newSO– newSO2011-08-31 10:59:42 +00:00Commented Aug 31, 2011 at 10:59
-
Not entirely in my opinion. If that's the case, that person should learn himself how to use the documentation to solve his problem, or purchase a proper book about the subject. Even after you master the language you will face problems and questions you dont have the answer for, but you'd have to use the resources available to solve your problem.TJHeuvel– TJHeuvel2011-08-31 11:02:56 +00:00Commented Aug 31, 2011 at 11:02
|
Show 2 more comments
3 Answers
It doesn't. It's only required if you want to use it.
From http://www.php.net/manual/en/functions.returning-values.php:
Values are returned by using the optional return statement.
5 Comments
newSO
will the functions results still be displayed like
hello world if i don't use the return statement?Rijk
Well, that depends on the content of the function and how it's implemented. The function itself can do an
echo "hello world";, for instance - that doesn't need a return value. But if you echo the return value, you need to return "hello world".newSO
your answer kind of confused me can you give an example if possible?
Rijk
Please read the documentation php.net/manual/en/language.functions.php or try some tutorials.
hakre
Probably worth to point out that functions that not explicitly return something will return
NULL. Often documented as @return void in docblock comments / API docs.