11 Vote

PHP: exit in include() should not cancel calling script

Question by Guest | Last update on 2021-03-25 | Created on 2019-03-06

I have structured my website in a way that I insert the page content in my index.php depending on the url. I am using the function include() for those includes.

In some of these included files, I also use an exit if I only want to execute the inserted script up to a certain extent.

Now the problem is that after invoking exit, my main page (the index.php) will be aborted, too. As a result, for example, the footer is no longer displayed on the corresponding pages. What can I do?

ReplyPositiveNegativeDateVotes
3Best Answer3 Votes

Just use "return" instead of "exit".

In fact, with exit, the calling parent script is also stopped. With return, only the script that is included.
Last update on 2021-03-25 | Created on 2019-03-06

ReplyPositive Negative
00 Votes

return gives program control back to the calling module. The execution continues with the expression that follows the call to the called module.

If called within a function, return immediately terminates the execution of the function and passes the parameter as the return value of the function. return also terminates the execution of code within eval() or a file.

If called in global space, the execution of the script ends. If the current script is included with include or require, only the execution of the included file ends and the value passed to return becomes the return value of the call to include / require. If return is called in the main script, the entire script ends. If the current script was called by the configuration directives auto_prepend_file or auto_append_file, the execution of this script will stop.
2019-03-07 at 22:40

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.