Home | Store | osCommerce Tutorials

PHP define() Function

The PHP define() function simply defines a constant. The constant values can only be strings and numbers. Note that the constant names do not need a leading dollar sign ($).

When studying the PHP codes of osCommerce files, we always came across the following strange names:

DIR_WS_IMAGES
DIR_WS_INCLUDES
DIR_WS_BOXES
DIR_WS_FUNCTIONS
DIR_WS_CLASSES
DIR_WS_MODULES
DIR_WS_LANGUAGES

If you opened the catalog/includes/configure.php PHP file, you should find the following define functions:

define('DIR_WS_IMAGES', 'images/');
define('DIR_WS_INCLUDES', 'includes/');
define('DIR_WS_BOXES', DIR_WS_INCLUDES . 'boxes/');

As mentioned before, the PHP define() is simply a function that defines a constant. In the above define() functions:

  • the file path of "images/" was defined for DIR_WS_IMAGES,
  • the file path of "includes/" was defined for DIR_WS_INCLUDES, and
  • the file path of "includes/boxes/" was defined for DIR_WS_BOXES

In other words, the file path of "images/" can be replaced with DIR_WS_IMAGES, the file path of "includes/" can be replaced with DIR_WS_INCLUDES, and the file path of "includes/boxes/" can be replaced with DIR_WS_BOXES.

Confused? Please see the following examples:

PHP define() Function Example 1:

<?php

define('DIR_WS_IMAGES', 'images/');

echo "Value of DIR_WS_IMAGES contant name is: " . DIR_WS_IMAGES;

?>

The output of the above PHP define() function example 1 should look like:

Value of DIR_WS_IMAGES contant name is: images/

Now we should have some basic concept of PHP define(), let's use it to display the osCommerce logo.

PHP define() Function Example 2:

<?php

define('DIR_WS_IMAGES', 'images/');

echo "<p>The osCommerce logo will be displayed below:</p>";

echo "<img src =" . DIR_WS_IMAGES . "store_logo.png>"

?>

If the osCommerce logo is in the "images/" directory, the output of the above PHP define() function example 2 should look like:

The osCommerce logo will be displayed below: