Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between `return;` and no return?

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...

like image 311
Rudie Avatar asked May 16 '11 13:05

Rudie


People also ask

Is return none the same as return?

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.

What is the difference between a function that returns none and a value returning function?

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.

What happens if there is no return statement?

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.

What is the difference between return type and return statement?

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).


1 Answers

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.

like image 156
user187291 Avatar answered Oct 06 '22 05:10

user187291