Phpdynamic

PHP and MySQL Programming Guides, Tutorials, & Articles

What are Constants?

What are Constants?

MAR 25

PHP Constants

A constant represents a literal value that will remain the same, for example the value for PI (3.14), the number of days in a week (7), or the number of ounces in a pound (16) all represent numbers that will never change. A constant can store a value similar to a variable, but that value cannot be modified during the execution of a script, once that constant is initialized. Constants are created using the built-in define() function, by assigning an identifier name to the constant along with a literal value.

There are a few limitations on naming identifiers for constants. Valid constant names must begin with a letter or an underscore (_) and subsequent characters can include both upper and lowercase letters or numbers. We’ll follow through with some examples:

<?php
  /* defining constants */
  define("PI", 3.14);
  define("TAX_RATE", 0.0875);
  $tax = 20.00 * TAX_RATE;

  /* expressions using constants */
  print(PI);
  print("<p>Tax due is : $tax</p>");
?>

The output:

3.14
Tax due is : 1.75

In the example above, we defined two constants called PI and TAX_RATE and assigned the values 3.14 and 0.0875 to each of the constants, respectively. A very common practice is to capitalize each letter in the identifier name and separate every word with underscores, like the TAX_RATE constant.

Why Use Constants?

Once a constant is defined, it can be readily referenced by its identifier name anywhere within the script. This functionality makes your scripts easier to read and modify. Suppose your working on a script for an online e-commerce website, that will perform various tasks like order processing and fulfillment. You could define constants to store the prices for each of the items available for sale and perform all the necessary calculations by referring to the constant identifiers instead of using the actual values. Now if you ever need to change the price of an item, you can simply change the value once in the define() statement for the respective constant as this will automatically update the price everywhere that constant is referenced. This method will save you the time and trouble of having to manually go through your script and update the prices line by line, if you used literals in several places instead of named constants.

Below is a list that highlights the similarities and differences between variables and constants:

  • A dollar sign ($) must prepend all variable identifiers, whereas constants do not require the dollar sign.
  • Variable and constant names must begin with a letter or an underscore (_) character, optionally followed by either upper or lowercase letters, numbers, or additional underscores.
  • Constants are defined using the define() function and once defined, they cannot be modified or undefined during the execution of a script.
  • Variables are created by simply assigning a value to them with the assignment operator (=), in addition variables can hold different data values at different time while the script is running.
  • Both constants and variables can be referenced anywhere within a script by simply refering to the corresponding identifier name.
  • Share/Bookmark

You can leave a response, or trackback from your own site.

1 Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>