Different Uses of return statement in PHP

Different Uses of return statement in PHP

In programming languages, the return statement is an important language constraint to return the value to the caller. In this tutorial, we’ll see different uses of return statement in PHP with examples.

I know that there is nothing special in creating this post only for return statement. But believe me, this will create new opportunities of using return statement in different part of the codebase and we’ll understand how dynamically we can use it in our day to day coding.

In most of the scenarios, we have seen this return statement inside the function definition only.

Let’s see the regular function and how to use return statement to return the value.

  <?php

function getId($id) {
    // code
    $value = $id?? 0;
    return $value;
}

echo getId(2);

In the above getId() function, we have checked whether $id is set or not using Null Coalescing Operator and then return results using a return statement.

In short, once the PHP interpreter reached at the return statement then the return statement immediately ends the current execution of the function, and the value passed as an argument to the return is returned to the function call.

Since return is a language construct and not a function, the use of parentheses surrounding its argument are not required at all.

Using return statement in Global Scope

Here is the first good use of return statement in global scope. We can use return statement directly inside the file which is included inside another file.

So, when we return value from one file then we can store the included file inside the variable and then value of the included file will be stored inside that variable.

Quite confusing, right?. Let’s see example first-

<?php
// file1.php

return array("name"=>"John", "address"=>"NJ, USA");
<?php

$employeeInfo = include_once("file1.php");

print_r($employeeInfo);
// OUTPUT

Array ( 
      [name] => John 
      [address] => NJ, USA 
)

So in file1.php, we have defined a return statement and in another file we included that file and assigned that to $employeeInfo variable and then we can perform whatever the action we want to take.

Yes, absolutely, we can assign include statement to a variable. for more reference check php.net official page. for more information check this link

return and exit() are not the same

Yes, you heard it correctly. Both are not the same because the first difference is that return is a language construct and exist() is an in-built function in PHP. Another difference is that using return just exits the execution of the current script while exit() function exists the whole execution.

So, what will happen when we just use return statement in one file and include that file in another.

// file1.php

<?php

include("file2.php");
echo 2;
?>

// file2.php

<?php

echo 1;
return;
?>

In above case, when we add return statement then only that current script execution gets stopped and compiler went back to the caller file.

// OUTPUT

12
// file1.php

<?php

include("file2.php");
echo 2;
?>

// file2.php

<?php

echo 1;
exit;
?>

In above case, we have replaced return with exit() and as per the functionality of exist(), it halts the further execution of the program. So file2.php is included and 1 is echoed then the compiler do not went back to file1.php and close the further execution.

//file1.php

<?php
include 'file2.php';

getId();
?>

//file2.php
<?php
return;

function getId() {
     echo 123;
}
?>

In the above example, as we know that return is the language construct all the code written after the return statement is still valid and can be executed.

conclusion:

We have seen different Uses of return statement in PHP but the fact is that we in general use only one form of the return statement i.e., in function only. with PHP 7 we can explicitly define which data we want to return from the function.