Is there a difference between:
function someMethod( $someArg ) { // some code return; }
and
function someMethod( $someArg ) { // some code // no return }
Both have NULL
as 'return value'. Is there a difference? Something PHP internally? Performance? Speed?
edit
I ask, because in Zend framework (in this video) they use return;
which seemed (seems) silly to me. However, you would think that the people behind Zend framework do know their PHP...
This will also return None , but that value is not meant to be used or caught. It simply means that the function ended successfully. It's basically the same as return in void functions in languages such as C++ or Java.
Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.
If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.
The return statement can be skipped only for void types. Not using return statement in void return type function: When a function does not return anything, the void return type is used. So if there is a void return type in the function definition, then there will be no return statement inside that function (generally).
php code
<?php function a() { echo 1; return; } function b() { echo 2; }
generated bytecode
.FUNCTION a ECHO 1 RETURN NULL RETURN NULL HANDLE_EXCEPTION .END FUNCTION .FUNCTION b ECHO 2 RETURN NULL HANDLE_EXCEPTION .END FUNCTION
so the explicit return statement generates one extra RETURN instruction. Otherwise there's no difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With