PHP: Data Types: Constants

Instead of using assignment operators like we do while declaring variables, constants are defined by define() function. Constants don’t use any $ sign like variables do. Define function takes two arguments name of the constant is first and the second is its value.

Constant names are case sensitive. Although it is not a requirement, convention carried over from other programming languages suggests that constants should be named in all upper case characters. The following example demonstrates the use of the define() function to specify a constant:

<?php define(‘your_age’, ’13′); ?>

Defined the constant value can be accessed by referring to the name. For example, if we define a string constant as follows:

<?php define(‘Hello’, “Hello World”); ?>

Now the value of above defined constant can be accessed anywhere in script by:

echo Hello;

 

(Related Tutorials)

Leave a Reply