Jump to content

PHP include question


RopeADope

Recommended Posts

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');
?>

 

Link to comment
https://forums.phpfreaks.com/topic/205844-php-include-question/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.