Home | Store | osCommerce Tutorials

PHP require() Function

The PHP require() function can insert the content of other PHP file into the current PHP file.

Why use require() function? There are some reasons why many PHP programmer like to use the require() function. Firstly it can make the main program shorter thus easier to maintain. Moreover the common files can be shared with other files.

The index.php (homepage) in the catalog directory of osCommerce should probably the first PHP file that many shop owners will opened when start studying the file structure of osCommerce shop. The following is the first line they will be seen in the index.php file:

catalog/index.php

require('includes/application_top.php');

To those osCommerce shop owners without any PHP knowledge, they will be scared by the above require() function of the PHP code.

How require() function works

For the above line of PHP code, the require() function simply inserts the file contents of application_top.php file into the index.php file. This can be illustrated with the diagram below:

PHP require function example

Here's just some contents of the application_top.php:

includes/application_top.php

<?php

// define the project version
define('PROJECT_VERSION', 'osCommerce Online Merchant v2.2 RC2a');

?>

The following PHP example shows how to use PHP require() function to insert the contents of application_top.php into the index.php file:

PHP require() Function Example 1:

<?php

require('includes/application_top.php');

echo "The version of your osCommerce shop is: " . PROJECT_VERSION;

?>

Provided that there is the file, application_top.php, in the includes directory, the output of the above PHP require() function example 1 should look like:

The version of your osCommerce shop is: osCommerce Online Merchant v2.2 RC2a

Look! The contents of the application_top.php file has been inserted into the index.php file.

Actually the PHP file (e.g. application_top.php) being inserted into the index.php can also use the require() function to insert other file into it's content. Let's see how it works this time.

Again, here some contents of the application_top.php file:

includes/application_top.php

<?php

// include server parameters
require('includes/configure.php');

// define the project version
define('PROJECT_VERSION', 'osCommerce Online Merchant v2.2 RC2a');

?>

Note:

The path of the inserted file is "include/configure.php", and NOT "configure.php". Because all files are finally inserted into index.php file therefore the file path should be reference from index.php, NOT application_top.php.

Here's some of the contents of the configure.php file:

includes/configure.php

<?php

define('DB_DATABASE', 'osCommerce');

?>

The situation is illustrated in the diagram below:

PHP require function

The following PHP example shows how to use require() function to insert the application_top.php and configure.php file into the index.php file.

PHP require() Function Example 2:

<?php

require('includes/application_top.php');

echo "The version of your osCommerce shop is: " . PROJECT_VERSION;
echo "<br /><br />";
echo "The name of MySQL Database is: " . DB_DATABASE;

?>

Provided that the application_top.php and configure.php have saved in the includes directory, the output of the above PHP require() function example 2 should look like:

The version of your osCommerce shop is: osCommerce Online Merchant v2.2 RC2a

The name of MySQL Database is: osCommerce

You will always encounter the require() functions when studying the PHP codes of osCommerce shop.