00 Votes

PHP: Abort within include - continue remaining script

Question by Guest | 2016-07-24 at 10:45

I have a PHP script in which another script is inserted using include('script.php'). So far, it is working like I want.

However, the script included with include() should be canceled and discontinued under some circumstances.

Of course, I could no formulate if-statements for each condition, but this would make the script much more complicated because of the brackets would have to appear again at the end.

So, what I need is something like an exit-command. I have also already tried to use the exit and break functions of PHP, but this always breaks the entire script and not only the included one. The original script from which the include was called, should be continued further in each case.

ReplyPositiveNegative
0Best Answer0 Votes

Just try to use "return". With this, you can write as much termination conditions for your script as you want. Here is an example:

// the included script

if (...) return; // termination condition 1
if (...) return; // termination condition 2

// here the actual code of the script begins
// ...
// ...

The actual code of the script will only be executed in that case if the "return" has not been triggered before. The superordinate script from which this script was called, will not be affected from that.

Another tip: It would be better to include the termination conditions in the super script because then you can save the processor from calling include(). This is better for your performance because the script file must only be searched for when it will also be used.
2016-07-25 at 08:53

ReplyPositive Negative
Reply

Related Topics

PHP: File Download Script

Tutorial | 0 Comments

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.