Defining Constants

While there is no expression or construct to define constants in the form of unmodifiable variables in PHP, you can nevertheless accomplish this objective by using defined values. Defined values should be used to replace all fixed values such as error codes, file format constants, special strings, and whatever else will have a special meaning to the program or library and not change during execution.

Defined values have the great advantage that they clarify the meaning of special values, while at the same time providing another level of abstraction:

 // read file type from input $file_type = fgets($file, 32); // decide what kind of file it is switch($file_type) { case FT_GIF_IMAGE: /* handle GIFs here */ break; case FT_PNG_IMAGE: ...

Get Web Application Development with PHP 4.0 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.