logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA
Irish Culture


dailyclick
All times in EST

Full Schedule
g
g PHP Site

BellaOnline's PHP Editor

g

The PHP Constant

Guest Author - Diane Cipollo

The PHP constant is very similar to a PHP variable in that it is used to store a value but, unlike a variable, the value cannot be changed. It is called a constant because; you guessed it, it stays constant throughout the PHP program. By using a constant you "lock in" the value which prevents you from accidentally changing it.

Secondly, if you want to run a program several times using a different value each time, you do not need to search throughout the entire program and change the value at each instance. You only need to change it at the beginning of the program where you set the initial value for the constant.

Let's take a look at an example where we use the define function to set the initial value of a constant to the state in the US that a business is located in. Then we can use this constant in an accounting program.

define("CONSTANT_NAME", "value");

define("STATE", "Ohio");

echo STATE;

In the example above, the define function begins with the function name define followed by parenthesis. Within the parenthesis are the name of the constant and the value to be assigned to the constant. Both are enclosed within quotation marks and separated by a comma. This is all followed by a semicolon.
  • You will notice that the value of Ohio is placed within quotation marks. This is because it is a character string. To store a numeric value you do not need quotation marks.

    define("TAX", 4);

  • You will also notice that, unlike PHP variables, there is no $ in front of the constant name.

  • Although it is not required, most programmers use all uppercase letters when naming constants to make them stand out from the variables in their program.

  • You do not use quotation marks when you use a constant in an echo statement. If you do, the echo statement will display the constant name.

    echo STATE; // will display Ohio

    echo "STATE"; // will display STATE




This site needs an editor - click to learn more!

Add The+PHP+Constant to Twitter Add The+PHP+Constant to Facebook Add The+PHP+Constant to MySpace Add The+PHP+Constant to Del.icio.us Digg The+PHP+Constant Add The+PHP+Constant to Yahoo My Web Add The+PHP+Constant to Google Bookmarks Add The+PHP+Constant to Stumbleupon Add The+PHP+Constant to Reddit




RSS | Editor's Picks Articles | Top Ten Articles | Previous Features | Site Map


For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor