Home  >  Article  >  Backend Development  >  How to Return from an Included Script in PHP?

How to Return from an Included Script in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-19 07:51:02857browse

How to Return from an Included Script in PHP?

Returning from Included Files in PHP

In PHP, when a script includes another script using the include() function, the execution of the including script is paused while the included script is executed. Normally, the execution of the including script resumes after the included script has completed. However, what if you want to return from the included script and resume execution in the including script?

The include() Function

The include() function is used to include and execute the specified file as part of the current script. When using include(), if the included script contains a return statement, the execution of the including script does not resume. Instead, include() returns NULL to the calling script.

Returning from an Included Script

To return from an included script back to the including script, you can use the exit() function. exit() immediately terminates the execution of the current script and returns the specified exit code to the caller. By using exit() in the included script, you can force the including script to resume execution.

Example

Consider the following example:

application.php:

<code class="php">$page = "User Manager";
if($permission["13"] !=='1'){
    include("/home/radonsys/public_html/global/error/permerror.php");
    exit();
}</code>

permerror.php:

<code class="php"><?php
exit();</code>

In this example, if the condition in application.php is not met, the permerror.php script is included. Inside permerror.php, the exit() function is used to terminate the execution of the current script (i.e., permerror.php) and return the execution to application.php. As a result, the script execution will resume at the line following include() in application.php.

Other Options

Besides using exit(), you can also use the require or require_once functions to include scripts. These functions behave similarly to include() but generate a fatal error if the file cannot be found or included.

The above is the detailed content of How to Return from an Included Script in PHP?. 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