- Skip the rest of the (included) file in PHP
- 4 Answers 4
- Is there a way to exit only the php file being included?
- 3 Answers 3
- Return from include file
- 5 Answers 5
- PHP exit() from within included script, exit parent script?
- 9 Answers 9
- PHP exit only current included script file from inside a function
- 3 Answers 3
Skip the rest of the (included) file in PHP
I’m including file inner.php in outer.php , I have a condition in inner.php on which I want to stop executing inner.php but NOT the whole script, i.e. I want to jump to the first line in outer.php after the inclusion of inner.php , and I don’t want to wrap all of the code in inner.php in an if statement. Is there a way to do this otherwise?
4 Answers 4
Just do return; or return($value); on top level of the inner.php file.
If called from the global scope, then execution of the current script file is ended. If the current script file was include()ed or require()ed , then control is passed back to the calling file. Furthermore, if the current script file was include()ed , then the value given to return() will be returned as the value of the include() call.
You can just call return in your include file, but if you’re having to do this then it suggests there is something wrong with your architecture. For example, consider this include file:
..included on the following page:
The output you’d get is: This is my include .
I recommended using a require to stop your page from executing if the include file is not found. I also agree that something is probably wrong with your architecture if you want to do this. Either split up your include files or do conditional testing in your include file.
How about having two files for inner. The first and the second part and place the condition on the second include?
Is there a way to exit only the php file being included?
So, I have a sidebar.php that is included in the index.php . Under a certain condition, I want sidebar.php to stop running, so I thought of putting exit in sidebar.php , but that actually exits all the code beneath it meaning everything beneath include(‘sidebar.php’); in index.php all the code would be skipped as well. Is there a way to have exit only skip the code in the sidebar.php ?
3 Answers 3
Do also be aware that it is possible to actually return something to a calling script in this way.
if your parent script has $somevar = include(«myscript.php»); and then in myscript.php you do say. return true; you will get that value in $somevar
Yes, you just use return; . Your sidebar.php file might look something like this:
I know this is a really old question, but I’ve recently taken over the code base of another developer who used exit religiously, meaning that the parent file that included various files had to be designed in such a way that the include of the module files were done at the end so it didn’t cut off the page. I wrote a small PHP script to replace all occurrences of «exit;» with «return;».
if($handle = opendir("path/to/directory/of/files")) < while(false !== ($file = readdir($handle))) < if("." === $file) continue; if(".." === $file) continue; $pageContents = file_get_contents($file); $pageContents = str_replace("exit;", "return;", $pageContents); file_put_contents($file, $pageContents); echo $file . " updated
"; > >
I hope this helps someone.
This question is in a collective: a subcommunity defined by tags with relevant content and experts.
Return from include file
in PHP, how would one return from an included script back to the script where it had been included from? IE: 1 — main script 2 — application 3 — included Basically, I want to get back from 3 to 2, return() doesn’t work. Code in 2 — application
$page = "User Manager"; if($permission["13"] !=='1') < include("/home/radonsys/public_html/global/error/permerror.php"); return(); >
Make sure you are calling return() at the top-level scope in the included script (i.e. not in a function)
What happens when you invoke return from your #3/included script? Can you post a sample of your code?
5 Answers 5
also gives the same result
This is one of those little-known features of PHP, but it can be kind of nice for setting up really simple config files.
See the PHP manual, especially Example #5 include and the return statement and its description.
It should be noted that in most cases this would be considered to be a somewhat bad coding style, as it treats the include file as a function. A more ‘standard’ approach would be to put functions with a proper name in the include file, include the file (without any return statement outside of functions) and then call the needed function.
Take care when using parenthesis (i.e., include(«x.php»); rather than include «x.php»), when it comes to return values. See Example 4 in the PHP documentation for include : php.net/manual/en/function.include.php
@Pacerier, as said, because it treats the include file as a function, thus relying on a rather unusual, language specific and not very explicit behavior. Things like that can easily be missed by the fellow developer diving in for maintenance (or cost some time to understand what’s going on), so if there is an equivalent, more common approach (like using explicit function calls in this case), it should be preferred.
@HenrikOpel That sounds like «Don’t use all the features of PHP, because other people don’t.» Doesn’t that set up a bad precedent for effectively removing features from a language, eventually boiling it down to the least common developer?
PHP exit() from within included script, exit parent script?
In PHP, if I use the include() or require() functions to start running code in another script, is there a way to terminate the parent script from within the child? So say I have this in parent.php:
require(‘child.php’); And this in child.php:
exit(); Will that terminate just child.php, or parent.php as well?
Is there a way to terminate parent.php from within child.php, without adding any further code to parent.php? It’s for an elaborate custom 404 error page in PHP which detects whether it’s been called by Apache using ErrorDocument, or whether a user has requested a page from our custom CMS which doesn’t exist. If it’s the latter, then I want to just require my 404.php (child) and output from that and not continue executing the parent. Anyone know if it’s possible to terminate a parent PHP script from within an included/required script?
It is helpful to understand how require/include work to explain the answers below. When you require/include a script, the code in that script gets inserted into the current script as if it were written that way. require/include do not execute anything; they simply pull code from an external file into the currently script. When the script is executed, the PHP engine doesn’t care where the code came from.
Please reconsidder your answer selection. Mark the answer of «Pekka» as solution since Pheonixs answer is not matching.
9 Answers 9
exit(); Will that terminate just child.php, or parent.php as well?
It will terminate the entire script.
If you don’t want to do that, but return to the including script, you can return from within an include.
If you return from within an include, won’t that specifically not terminate parent.php and continue executing from after the include statement?
Use ‘return 0’ ou ‘return true’. Only ‘return’, if there is something more below the line it will try to return as a value.
It’s important to differentiate between php files and cpu processes. Including a script does not create a new process. It just extends or augments the code that is being run. Using return vs exit is about program execution, not about the underlying process. Ending the process is the result of the code terminating whether by running out of code to execute, or by hitting an exit statement (implicit vs explicit).
You are looking for return ; command. This will terminate execution of only the child.php, and parent.php will be processed after that.
You can use return if you need to exist included file but continue from main script.
return returns program control to the calling module.Execution resumes at the expression following the called module’s invocation.
If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.
If called from the global scope, then execution of the current script file is ended. If the current script file was included or required,then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call. If return is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini,then that script file’s execution is ended
PHP exit only current included script file from inside a function
I know I can use return to exit current script file, but what if we want to exit current script from inside a function? For example, we define a exitScript() function that prints closing html tags and exits current script file, but if we use return it only exits from current function. Also exit() terminates whole script not only current file.
You should only use return to return from a function. It is not ment to be a mechanism to exit a script.
@RiggsFolly There’s a way to exit whole script from within a function, so it logically can/should be a way to exit only current script file from inside a function.
3 Answers 3
You can use Exceptions to do this, not particularly elegant, but this should do what your after, replace these methods.
public function fn1() < try < $fn2 = $this->fn2(); > catch ( Exception $e ) < >echo 'I want this to be displayed no matter what!'; > public function fn4() < $random = rand(1, 100); if ($random >50) < return true; >else < // I want to exit/break the scirpt to continue running after // the $fn2 = $this->fn2() call in the $this->fn1() function. //exit(); throw new Exception(); echo "This shouldn't be displayed."; > >
Thanks it’s a good solution. Also if we put last code inside a finally block it runs even fatal error occurs that is more than what I wanted.
You can return from an included file to the ‘main file’, skipping execution of the remaing part after the return .
So this will output: ABXend , skipping C
This needs to manually check all x() function calls and put return after them. Also we need to put return after calling functions that they call x() because we also can’t use return inside those functions:)
I know I can use return to exit current script file
The above statement is not true, as has already been pointed out.
One solution for your question is to make use of. goto !
But you don’t call goto within a function. You would call your exitScript() function to print the closing HTML tags, and then immediately afterwards, use the goto operator to jump to the end of the included file.
A lot of developers would absolutely balk at the use of goto , but for certain situations and use-cases (and if you are an experienced developer and realise what you are doing), it can actually function as an efficient solution.
Probably though a better solution would be to break the included file up into more logical sections, and only include what you need in this case, and don’t include the code that you don’t want to run.