O'Reilly logo

PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition by Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

About Constants

Constants are a specific data type in PHP that, 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. Constants can be assigned any single value—a number or a string of characters.

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 special syntax as well:

define ('USERNAME', 'trout'); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required