1

I've seen lots of instances when a "return" is included at the end of function in PHP yet passes no value and therefore would return null. I know it also exits the function, but is that necessary or is the return superfluous/just a way for developers to communicate that the function doesn't return any value?

A code example:

// Check to see if the request is a HXR call

if (request::is_ajax())
{
  // Send the 403 header
  header('HTTP/1.1 403 Forbidden');
  return;
}

See? Couldn't you take out the return; and have exactly the same functionality? Or, if there were other functions following this if statement, would they not run because the computer would be stuck in the if statement?

2

2 Answers 2

2

Correct. You do not need the return statement. It is only necessary when returning information out of the function, or to stop execution of the function. Otherwise the function will stop when it goes out of scope (reaches the closing bracket).

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

Comments

1

Developers use return to declare the end of the function so even when there is code after the return it will not be executed and it will be escaped. And it's not obligatory.

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.