Return from include file

Return from include file

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Return Value Trap in PHP Includes

You are running into a fundamental misunderstanding of how include and require work within the PHP execution model. When you execute an include statement, you are invoking another script that runs in its own scope. If that included file contains any code—whether it's logic, output, or even a simple echo—the main script pauses and waits until that included script finishes executing completely before moving to the next line.

This is why your attempt to use return() inside the included file fails. The include statement does not treat the included file as a function call; it treats it as a sequential block of code. There is no execution flow returning from the inclusion back to the caller in the way you might expect from a method within an object or a standard function.

To fix your specific scenario, you must change how the permission check and error handling interact with the main script's logic. The most robust pattern for this situation is to make the included file responsible for terminating the entire request immediately. Instead of trying to signal the main script to "go back," you tell the web server to stop processing the current page entirely.

You can achieve this by including a helper function or using a dedicated exit mechanism within your error file. One common approach is to define an exit call at the very top of your included file. However, since you are loading a file from a path like /home/radonsys/public_html/global/error/permerror.php, you need to ensure that this file is designed specifically for termination.

Here is how you should structure your main application script:

$page = "User Manager";

// Check permissions before attempting inclusion
if ($permission["13"] !== '1') {
    // The included file must handle the exit logic
    include("/home/radonsys/public_html/global/error/permerror.php");
    // This line is technically unreachable if the error file calls exit()
    return; 
}

// Execution continues here only if permissions are valid
echo "Welcome to the User Manager, {$page}";

Now, look at how your included error file should be structured. It cannot simply display an error message and then rely on the caller to stop execution. It must explicitly terminate the script:

<?php
// permerror.php

$errorCode = "PERM_DENIED";
$errorMessage = "You do not have permission to access this section.";

// Display the error (HTML, JSON, etc., depending on your setup)
echo "<h1>Access Denied</h1>";
echo "<p>{$errorMessage}</p>";

// CRITICAL: Terminate the script immediately.
// This ensures no code in the main script runs after this point.
exit(0); 
?>

The exit() function (or its alias die()) is the only reliable way to break out of an inclusion context. It sends a response header, flushes any output buffers, and halts execution immediately. This prevents potential security issues where sensitive data might be printed after the error has already been shown, or where subsequent code in the main script might accidentally execute.

If you attempt to return control without calling exit(), the included file finishes, and the main script continues running the line immediately following the include statement. In your example, that line would be the return();. While a return() inside the main script will exit if it is at the top level of a request (which it usually is), relying on this sequence makes the flow harder to debug and maintain.

In frameworks like Laravel, this pattern is handled differently because they use dependency injection and controller methods rather than raw includes. However, for legacy codebases or simple scripts where you are manually including files from a global directory, explicit termination within the included file is the standard convention. It guarantees that once an error state is reached, the request lifecycle ends cleanly without ambiguity about execution flow.

Our network