RopeADope Posted June 25, 2010 Share Posted June 25, 2010 Just had a few PHP include questions. Is there a "best practice" when using PHP include, inlcude_once, require(e.g. when to use each one)? When including a file, does it have to be .php and should it be "echoed out"? Q2 Example: File to be included <!-- somefile.txt --> <html> vs. <?php echo "<html>"; ?> File doing the include //index.php <?php include('inc.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/205844-php-include-question/ Share on other sites More sharing options...
Adam Posted June 25, 2010 Share Posted June 25, 2010 As the file is included it will be parsed like any other PHP file. If there's static HTML in there that will output like any other static HTML. If include fails it will only raise a warning; require a fatal error. So if you *require* the file be included (and stop execution if it isn't), then you'd use require. Generally I always use require over include. include_once / require_once are the same, except they will include the file only once (as the name implies). If you're including a file full of functions for example, and there's a chance it may get included again (and produce re-declaration errors), you'd want to use include/require_once. include/require_once are slightly more resource heavy though as they have to check if the file has been previously included. If you plan out your application / code properly, you should mostly be able to escape using it. Funnily enough the manual explains all of this.. http://php.net/manual/en/function.include.php Quote Link to comment https://forums.phpfreaks.com/topic/205844-php-include-question/#findComment-1077140 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.