Broad Network

Constant in PHP

Some features of PHP Entities – Part 1

Foreword: In this part of the series, I explain how to define a constant in PHP.

By: Chrysanthus Date Published: 28 Jan 2014


This is part 1 of my tutorial series, Some Features of PHP Entities. In this part of the series, I explain how to define a constant in PHP. In this series, I explain some of the important features not explained so far, in my volume, PHP Course.

Note: the output of the code samples in this series are to the browser.

Note: in this article, if you cannot see any text or piece of code or if you think something (e.g. an image) is missing or link does not operate, or want to comment, just contact me at .

This series is part of the volume, PHP Course. You should be reading the different series of the volume in the order given. Click the link, “PHP Course” below to see where this series fits into the volume.

A class, an object, a function, a variable, an array, are examples of entities.

A constant is a variable, whose value cannot change during the execution of the script (program). The constant variable does not have the $ sign.

Defining a Constant
You can define a constant using the built-in function, define() or the specifier, const. So you can have something like:

    define("cn", 5);


    const cn = 5;

Whether you are using the define() function or the specifier, const, if the value is a string it has to be in quotes. Above, the value is a number, so it is not in quotes. You get the value of a constant just as you get the value of any variable but without preceding the constant variable with $. Read and try the following code:


    define("cn", 5);
    echo cn, '<br>';

    const kn = 6;
    echo kn, '<br>';


The define() Function
The full syntax of the define() function is:

    bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )

It returns TRUE if it successfully defines a constant or FALSE if it fails. $name is the name of the constant variable such as cn or kn above. $value is the value such as 5 or 6 above. Only scalar and NULL values are allowed. Scalar values are integer, float, string or boolean values.

$case_insensitive is optional; if set to true, the name of the constant is case insensitive. That is, cn as name is the same as CN or cN or Cn. If set to false, the name of the constant is case sensitive.

The Scope
Constants may not be redefined or undefined once they have been set. A constant is of the global scope. So if you define a constant inside a function definition, it will be seen inside the function definition and outside the function definition. If you define a constant outside the function definition, it will be seen outside the function definition and inside the function definition (without any special coding). Read and try the following code:


    const KN = 10;
    echo KN, '<br>';

    function fn()
            echo KN, '<br>';
            define("cn", 20);
            echo cn, '<br>';


    echo cn, '<br>';


The statement, “const cn = 20;” does not operate in the function definition of my system; that is why I have used “define("cn", 20);”.

Constant in a Class
Click the link titled, “Understanding Object Oriented Programming in PHP” and go to the part in the series titled, “PHP Class Constant and Static Members” to learn how to code the constant in PHP OOP.

That is it for this part of the series. We stop here and continue in the next part.


Related Links

Basics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
PHP Strings
Date and Time in PHP
PHP String Functions for Website Design
Advanced Course
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Namespace
PHP Web Application

More Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course



Become the Writer's Fan
Send the Writer a Message