How to Return Data from Included PHP Files?

Mary-Kate Olsen
Release: 2024-10-19 07:50:30
Original
541 people have browsed it

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>
Copy after login
<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>
Copy after login

Solution:

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

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

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!