News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Constants

Started by VelMurugan, May 20, 2009, 05:04 PM

Previous topic - Next topic

VelMurugan

Constants

Constants just as variables are used to store information. The main difference between constants and variables is that constant value can not be changed in the process of running program. It can be mathematic constants, passwords, paths to files, etc. By using a constant you "lock in" the value which prevents you from accidentally changing it. 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.

Have a look at the example where we use the define function to set the initial value of a constant:

<?php
// first we define a constant PASSWORD
define("PASSWORD","admin");

echo (
PASSWORD);                  // will display value of PASSWORD constant,  i.e. admin
echo constant("PASSWORD");   // will also display admin
echo "PASSWORD";                 // will display PASSWORD
?>


PHP also provides a number of built-in constants for you. "__FILE__", for example, returns the name of the file currently being read by the interpreter. "__LINE__" returns the line number of the file. These constants are useful for generating error messages. You can also find out which version of PHP is interpreting the script using the "PHP_VERSION" constant.