0

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.php Commented Aug 31, 2011 at 10:48
  • what is with the negative rating? Commented Aug 31, 2011 at 10:54
  • Because its not really a question, commonly known and well documented. Commented Aug 31, 2011 at 10:58
  • Its still a valid question not every one knows everything about php or do they? Commented 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. Commented Aug 31, 2011 at 11:02

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

will the functions results still be displayed like hello world if i don't use the return statement?
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".
your answer kind of confused me can you give an example if possible?
Please read the documentation php.net/manual/en/language.functions.php or try some tutorials.
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.
1

If your function doesn't need to return anything, then it doesn't have to.

Saying that, I tend to return a boolean as to whether the function completed its job or not. Other times, I just let it move to its next job.

Comments

0

It is totally up to you. If you want your function to return some result to use elsewhere, then you should have it return something. Otherwise, if your function does something, but you don't need a result, then don't have it return anything.

It is your choice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.