newSO
newSO

Reputation: 13

PHP when and when not to use a return statement inside a user defined function

I was wondering if a user defined function will always needs a return() statement and if not when is a return() statement not required?

Upvotes: 0

Views: 134

Answers (3)

Layke
Layke

Reputation: 53136

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.

Upvotes: 0

James
James

Reputation: 5169

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.

Upvotes: 1

Rijk
Rijk

Reputation: 11301

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.

Upvotes: 1

Related Questions