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
echo HELLO; echo WIDGET_PRICE;
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:
Another useful predefined constant is
PHP_VERSION, which holds the current version of the running PHP engine:
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:
...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
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.