Back to Top

How to use CONST keyword in PHP

how-to-use-const-keyword-in-php

A constant is one kind of variable which holds a value, but it is more like a function because a constant is immutable.Once a constant is declared, it does not change.Usually, You are using the define() function to define constants in PHP but you know after PHP 5.3, you can use PHP keyword const to define constants.

The Constants have the same rules as PHP variables except that constants don’t have the leading dollar sign($).Once Constants are defined, its globally accessible.There is no need to re-declare constants in each new functions or in PHP files.

In PHP 5, constants can be optimized by changing them to const keyword which are stored as part of the class variables and also cached by opcode caches.Constants can contain only scalar data like boolean, integer, float and string, you can not define array or object as constant before PHP 5.7 version.PHP 5.7 supports array to define as constant.

How to use define() function?

First, Let’s see the default define() function

Here is the explanation about define,

1. “CONSTANT_NAME” is a string and mandatory.
2. constant value is any valid value of constant excluding arrays and objects.
3. case_sensitive is a boolean (true/false) and is optional. The default is true.

With the newer version of PHP 5, you can now define constants inside classes with keyword const.Class definitions can now include constant values and are referenced using the class. Like Static members, constants belong to the class but not to the instance of the class.To access constants is similar to accessing static members.

How to define and use a Class Constant

First, Let’s see the const keyword syntax

You can define constants in the class. Value of the constant will remain unchanged.Value must be a constant value and can not be a variable, class, property.

NOTE: Class constants are always case-sensitive.

ECHO:

Let’s have a look with the use of Const keyword

This code display “triangle” and “circle”. It demonstrates the ability to access the constant both from inside a class method with the self-keyword and via the class name “MyConstclass”.

Difference between define and const

Now, You have two methods to define constants but you don’t know, what is different between them yet. So, Let’s understand the difference between PHP define() function and the const keyword:

1. The define() function can be used in PHP4 and above whereas you can not use the const keyword in below PHP 5.3.0 versions.

2. The define() function can be called from anywhere, However const keyword defines constants at the compile time and const keyword can be used within class.

3. The define() function name is not case-sensitive constant.

4. Constants that are defined with define() function are not cached. However, the const keyword is cached.

NOTE: You can use the function get_defined_constants() to get a list of all defined constants.

As per the name, constants are constant and can be neither changed nor removed after it is defined.If you use an undefined constant, PHP display the name of the constant itself(CONSTANT) because it is passed as a string.

It is common practice in most programming languages including PHP, to use upper-case letters to declare constants. Although It is not mandatory, If you wish, you can define your constants as case-insensitive but if you are following some coding standard, go with case-sensitive constants.

If you know of more about define() function and const keyword, share with me via comments. If you enjoy reading this article, do share it on Facebook and Google plus.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How To set up Signup Bonus in X-Cart

Posted on 12 years ago

Bhumi

Post via e-mail in WordPress

Posted on 12 years ago

Bhumi