PHP Constants

Learn how to create PHP constants with define(), as well as how to use constants in your PHP scripts. Also looks at predefined constants.

You might be familiar with using PHP variables to store values in PHP scripts. PHP constants are similar, but have a few important differences, as you'll see in this article.

PHP constants explained

Like variables, PHP constants can store values. However, once a constant has been created, its value cannot be changed while the script runs.

Constants are useful for storing data that doesn't (and shouldn't) change while the script is running. Common examples of such data include configuration settings (such as database usernames and passwords) and fixed strings of text to display to visitors (such as "Please login to continue").

Constants are also often used to represent integer values with special meanings in a particular context, such as error codes and flags.

How to create a constant

PHP constant names follow the same rules as PHP variable names. The only difference is that constant names don't start with a $ (dollar) symbol, while variable names do.

PHP constant names are case-sensitive. Usually, constant names use all-uppercase letters, with underscores to separate words within the name.

To create a PHP constant, use the following syntax:


define( "CONSTANT_NAME", constant_value );

"CONSTANT_NAME" is a string holding the name of the constant to create, while constant_value is the value that the constant will hold. Here are a couple of examples:


define( "HELLO", "Hello, world!" );
define( "WIDGET_PRICE", 29.99 );

The first line of code creates a PHP constant called HELLO with a string value of "Hello, world!", while the second line creates a constant called WIDGET_PRICE with a numeric value of 29.99.

PHP constants can only hold scalar values — these include numbers, strings of text, and boolean values. They can't hold arrays or objects, like variables can.

You can only define a constant once within a script. If you try to redefine a constant, the PHP engine generates an error.

Using constants in PHP

You access a PHP constant in exactly the same way as you use a variable. To use a constant's value, simply write the constant name. The following example displays the values of the HELLO and WIDGET_PRICE constants:


echo HELLO;
echo WIDGET_PRICE;

Predefined constants

PHP features a large number of built-in, predefined constants holding various useful values. Some of these are always available, while other constants become available when certain PHP extensions are enabled.

For example, the PHP constant M_PI holds the mathematical constant Pi. The following code displays this value:


echo M_PI;

Another useful predefined constant is PHP_VERSION, which holds the current version of the running PHP engine:


echo PHP_VERSION;

The PHP website has a list of built-in predefined constants.

You now know what PHP constants are, as well as how to create constants and use them. Happy coding!

Learn PHP With Ease!

Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:

  • Set up PHP on your computer
  • Use strings, arrays, functions and objects
  • Create interactive Web forms
  • Handle cookies and sessions
  • Work with files on the server
  • Build database-driven sites with MySQL
  • Send emails from your scripts
  • Create images on the fly with PHP
  • Work with regular expressions
  • Write robust, secure PHP applications

...and lots more!

“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz

Buy Beginning PHP 5.3 now from Amazon.comBeginning PHP 5.3 or Amazon.co.ukBeginning PHP 5.3.

Follow Elated

Related articles

Responses to this article

There are no responses yet.

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page