The return Statement PHP

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 value of the expression will be passed back.
demonstrates this idea by taking a string and returning it wrapped in bold tags.

A Simple Function Using return

function makeBold($inputText)
$boldedText = "<B>";
$boldedText .= $inputText;
$boldedText .= "</B>";
print("This Line is not Bold<BR> );
print(makeBold("This Line is Bold") . "<BR> ");
print("This Line is not Bold<BR> ");

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics