About Constants

Constants are a specific data type in PHP which, unlike variables, retain their initial value throughout the course of a script. In fact, you cannot change the value of a constant once it has been set.

To create a constant, you use the define() function instead of the assignment operator (=) used for variables.

define ('NAME', 'value');

Notice that, as a rule of thumb, constants are named using all capitals, although this is not required. Most importantly, constants do not use the initial dollar sign as variables do (because, technically, constants are not variables).

Printing constants requires one of two techniques:

echo 'Hello, ' . NAME;

or

echo 'Hello, ', NAME;

You cannot print constants using echo "Hello, NAME" as ...

Get PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.