4.2. The return Statement

At some point a function will be finished, ready to return control to its caller. This happens, for example, when execution reaches the end of the function's block of code. Execution then picks up directly after the point where the function was called. Another way to stop execution of the function is to use the return statement.

You may have multiple return statements in your function, though you have to consider how this reduces the readability of your code. Multiple return statements can be a barrier to understanding the flow of execution. Ideally, functions should have one way in and one way out. In practice there are cases when multiple return statements are acceptable.

If you follow return with an expression, the ...

Get Core PHP Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.