Search This Blog

Wednesday, March 31, 2010

The Include and Require Statements

Another way to make your code easier to use and maintain is to use include files. Actually, you've used them several times already and have seen how easy it is to bring external files into the current script and run them. All you have to do is use either the include or require constructs (remember, they're language constructs, not functions, even though they are called in a similar fashion) with the name of the file you want to bring in, and that's that. The only difference between the two is how they handle failure to bring in the external file. A failure of include results in a warning; a failure of require results in a fatal error.


There are a few fine points about how they work:
  • You include or require a file by inserting the construct within PHP code, but parsing drops out of PHP mode and into HTML mode at that point, so you still have to put the code in the included file inside proper PHP delimiters to make it run.
  • You may include or require a file from within a function, but if so, any variables created in the code in the included file have local scope within the function.
  • The PHP script within the include file is evaluated (run) when it is included. Any PHP variables that have values are available in the main file, just as though the entire block of code had been directly written inside the file.
Here are a few examples of how to write these constructs in your code (you can include full absolute or relative paths if you like):


include("filename")
require ("filename");

For example, to include the file test.txt, you would type:


include("test.txt");

If the file test. txt contained the text "Hello", then this text would be included on the Web page, just as though it had been coded directly into it.


Interestingly, within the include or require functions you can also add variable names or concatenate them to the name of the filename, inside the brackets, as long as it creates a legal file name:


$Name ="1";
include ("test" . $Name . ".txt");

The result of this concatenation is test1.txt, and if there is a file of that name, it will be included—otherwise an error is generated.


So when are include files handy? Use them to:
  • Include text files in the page.
  • Define variables and/or constants and details of certain error messages.
  • Insert the values of HTTP variables in the page.
  • Execute a separate PHP script (even when it may return nothing).
  • Place commonly used functions that you need but don't want to define in every page.
Include files are useful in that they can be added directly to a file, but you can create code that decides whether that file should be included or not. That's exactly what you're going to do in the next example.


Try it Out: Conditional Includes
Start example
  1. Start up your Web page editor and type in the following words:
    File One included
    
  2. Save this as a text file and call it file1.txt.
  3. Close it and create a new text file and type in the following:
    File Two included
    
  4. Save this as a text file and call it file2.txt.
  5. Now close that file, start another new file, and type in the following:
    <html>
    <head><title></title></head>
    <body>
    <?php
    if (isset($_POST['posted'])) {
       $choice = $_POST['choice'];
       if ($choice <> "") {
          include("file" . $choice . ".txt");
          echo "<hr>";
       }
    }
    ?>
    <form method="POST" action="include.php">
    <input type="hidden" name="posted" value="true">
    What file do you wish to include?
    <select name="choice">
    <option value="1">file one</option>
    <option value="2">file two</option>
    </select>
    <br>
    <br>
    <input type="submit" value="Get Text">
    </form>
    </body>
    </html>
    
  6. Save this as include.php in the same place as the two text files.
  7. Open include.php in your browser and make a choice. 


End example

How it Works

First a little form is created to offer the user two choices in a drop-down box named "choice". Then, when the following code runs, it simply checks to see which value was chosen (if you chose file one, for example, the value provided was 1) and uses that value to construct the filename to include.


<?php
if (isset($_POST['posted'])) {
   $choice = $_POST['choice'];
   if ($choice <> " ") {
      include("file" . $choice . ".txt");
      echo "<hr>";
   }
}
?>

Of course, you'd already made the text files file1.txt and file2.txt and entered a little bit of text in them (otherwise you'd have gotten an error message. Did you?).