Opencart – PHP Coding (Constants)

This is the first part of what will be many articles on PHP coding as it involves Opencart. I was hoping to share some of my own knowledge on the subject as well as others along the way. This can be for the beginner coder that is just starting out in the world of PHP , or for the more seasoned coder. Some folks don’t really care to know about the inner workings of PHP but for those that are say for example “going to design their own theme” , it’s good to at least have a basic knowledge of PHP. It would definitely help you understand the magic of Opencart and expedite your development as a whole.

PHP Constants

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren’t actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*



// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR", "something more");

// Invalid constant names
define("2FOO",    "something");

// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__", "something");

Now let’s look at an example of where Constants can be found defined in Opencart. In your Opencart store directory open /config.php after you run the install and you will see quite a few constants. Here is an example:



define('HTTP_SERVER', '*******************');

define('HTTP_IMAGE', '*******************');

define('HTTP_ADMIN', '********************');


define('HTTPS_SERVER', '*******************');

define('HTTPS_IMAGE', '********************');

// DIR

define('DIR_APPLICATION', '*******************');

define('DIR_SYSTEM', '*******************');

define('DIR_DATABASE', '*******************');

define('DIR_LANGUAGE', '*******************');

define('DIR_TEMPLATE', '*******************');

define('DIR_CONFIG', '*******************');

define('DIR_IMAGE', '*******************');

define('DIR_CACHE', '*******************');

define('DIR_DOWNLOAD', '*******************');

define('DIR_LOGS', '*******************');

// DB

define('DB_DRIVER', 'mysql');

define('DB_HOSTNAME', 'localhost');

define('DB_USERNAME', '*******************');

define('DB_PASSWORD', '*******************');

define('DB_DATABASE', '*******************');

define('DB_PREFIX', '');


The **************** is hiding my own details for security reasons. Since this config.php file is included at the top of each and every single Opencart page on the front end then you can use the constants at will. Here is an example of how you could use the constant DIR_IMAGE:

		} elseif (!empty($category_info) && is_file(DIR_IMAGE . $category_info['image'])) {

This is taken from category.php

When using a constant you don’t have to use a $ like you would in a variable. The important thing is that the way the constant is spelled matches across the board. Remember that a constant is case sensitive too.


You can also just use

<code><span class="kwd">const</span><span class="pln"> FOO </span><span class="pun">=</span> <span class="str">'BAR'</span><span class="pun">;</span></code>

However, remember that CONST cannot be used to conditionally define constants. It has to be used in the outermost scope.

Looking for quality OpenCart Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *