Home  >  Article  >  Backend Development  >  How to Return Data from Included PHP Files?

How to Return Data from Included PHP Files?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-19 07:50:30542browse

How to Return Data from Included PHP Files?

Returning from Included Files in PHP

When including external PHP files, there may be a need to return specific data or exceptions back to the script where the included file was invoked. The standard return() statement does not always suffice in this scenario.

To address this issue, PHP provides a lesser-known feature that allows returning values from included files. Consider the following code scenario:

<code class="php">// main script
$page = "User Manager";
include("application.php"); // script 2

// ...other code...</code>
<code class="php">// application.php (script 2)
if($permission["13"] !=='1'){
    include("/error/permerror.php"); // script 3
    return(); // this does not return to script 2
}</code>

Solution:

Instead of using return(), the script 'includeme.php' can explicitly return a value:

<code class="php">// includeme.php (script 3)
return 5;</code>

This value can then be captured in the main script using the require() function:

<code class="php">// main script
$myX = require 'includeme.php'; // get returned value from script 3</code>

This approach allows for controlled returning of data from included files, providing flexibility in code execution and parameter passing.

The above is the detailed content of How to Return Data from Included PHP Files?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn