Website HostingDomain RegistrationsWebsite ScriptsWebsite TemplatesWeb Hosting AffiliateWebsite ConsultingContact Us
Website Hosting
PHP Documentation

PHP

- Documentation -

Chapter 9. Constants

A constant is a identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except the magic constants which aren't actually constants). A constant is case-sensitive by default. By convention constant identifiers are always uppercase.

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

Note: For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff).

The scope of a constant is global--you can access it anywhere in your script without regard to scope.

Syntax

You can define a constant by using the define()-function. Once a constant is defined, it can never be changed or undefined.

Only scalar data (boolean, integer, float and string) can be contained in constants.

You can get the value of a constant by simply specifying its name. Unlike with variables, you should not prepend a constant with a $. You can also use the function constant(), to read a constant's value, if you are to obtain the constant's name dynamically. Use get_defined_constants() to get a list of all defined constants.

Note: Constants and (global) variables are in a different namespace. This implies that for example TRUE and $TRUE are generally different.

If you use an undefined constant, PHP assumes that you mean the name of the constant itself. A notice will be issued when this happens. Use the defined()-function if you want to know if a constant is set.

These are the differences between constants and variables:

  • Constants do not have a dollar sign ($) before them;

  • Constants may only be defined using the define() function, not by simple assignment;

  • Constants may be defined and accessed anywhere without regard to variable scoping rules;

  • Constants may not be redefined or undefined once they have been set; and

  • Constants may only evaluate to scalar values.

Example 9-1. Defining Constants

<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.
?>



Go back to index



Have unlimited websites hosted for only $5.95 a month! Check out our new Hosting Plans!


Note: Documentation is made available for our clients. The application's homepage is located at PHP.net





All pages Copyright © 2002-2008 by Flux Services, Inc. All rights reserved.
All trademarks used are properties of their respective owners.
Refund Policy  Privacy Statement  Service Agreement


Google   Yahoo   MSN   AOL   Altavista   Lycos   Ask Jeeves
Site Hosting  Resource Directory  Website Traffic
< >