All About Includes

April 11th, 2008 | by admin | |

When building your applications, you will commonly have files containing functions (or classes containing functions), and configuration data such as

Ever had information that you thought could be useful in two or more places in your application, but have had to repeat that same information in two or more places because you didn’t know what else to do? Well, enter the world of includes.

<?php
//include syntax
include("my_file.php");
?>

There are four different types of includes in PHP.

  1. include
  2. include_once
  3. require
  4. require_once

The different between include and require is simple. If you call your include file by the ‘include’ method, and the file does not exist, PHP will pull a warning (which depending on your PHP settings may or may not be shown on screen). If however, you call your include file by the ‘require’ syntax, PHP will pull a fatal error, which will be shown on screen.

The different between an ‘include’ and ‘include_once’ is if the same file is included more than once in your script, ‘include’ will include it again, ‘include_once’ will not. ‘include_once’ is kind of like asking the question: “Has this file already been included? No, ok include it, Yes ok, ignore.”

Real world example

Your application may use a file called ‘products_class.php’. To use the class file in another file, you simply include it! Now your new file can call any of the functions in the products_class file.

Post a Comment