ASP: 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. If we attempt to re-assign the value of the constant we’ll get a run time error.

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.

To declare a constant in VBScript we use the Const keyword. Have a look at the following example:

Const myConst = "myText"

'it is allowed to declare a few constants on the one line

Const PI = 3.14159, Wg  = 2.78

Next example demonstrates the advantage of constants: during the calculations with number PI, you can type in your code only constants names instead of typing this number each time.

...

vCircle = PI * vRadius^2
admin

admin

Leave a Reply

Your email address will not be published.

PHP: 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.

admin

admin

Leave a Reply

Your email address will not be published.