A Web Design Community curated by Chris Coyier

Code Snippets Gallery

Code Snippets > PHP > PHP Include Submit one!

PHP Include

Can be used multiple times:

<?php include("navigation.php"); ?>

Once only:

<?php include_once("navigation.php"); ?>

Including from the root:

<?php
   $path = $_SERVER['DOCUMENT_ROOT'];
   $path .= "/common/header.php";
   include_once($path);
?>

Reference URL

4 Responses

  1. David says:

    Just a point to make, the include, incude_once and require equivalents are not functions and therefore do not need the parenthesis around the pathname.
    I am unsure of the origin of this bad habit.

  2. Dyllon says:

    well David, it’s obviously an alternative to use parenthesis and completely optional, it doesn’t make it any more correct or incorrect. Someone might have the same argument about spaces before and after brackets and colons in CSS.

    • David says:

      I know it is optional but I bet that newcomers believe that require and include are functions because of the parenthesis. It does add confusion in my opinion.
      If you have a look at the official documentation you can see the official examples do not use parenthesis and it also cautions you on the use of parenthesis in certain situations.
      http://us.php.net/manual/en/function.include.php

  3. Fraz Ahmed says:

    Although it does not create any difference whether using parenthesis or not. But really it can make difference in some situation. Visit David provided link again and give attention to example #5:

    http://php.net/manual/en/function.include.php

    I also have written an article, describing the difference between include and include_once, require etc.

    http://www.techmug.com/php-basics-for-beginners/

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.