Constants

From Computer Science Wiki
Jump to: navigation, search
Programming basics[1]

In computer programming, a constant is an identifier with an associated value which cannot be altered by the program during normal execution – the value of the constant is, well, constant[2].

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script. A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.[3].

Constants are used when you want to prevent changes to data in a program. This usually happens in larger programs, with multiple programmers. Constants also provide a measure of security, especially in web-based applications. In lower-level languages, constants can provide more efficient use of memory than variables.

Example

Below is an example of declaring a constant in PHP:

 1 <?php 
 2 
 3 # this file is used to demonstrate constants in PHP
 4 
 5 define("SCHOOL_YEAR",     "2016 - 2017");
 6 
 7 # for the remainder of the running of this program, the value of SCHOOL_YEAR will be 2016 - 2017
 8 # If we wanted to CALL (use) a constant, we might use the following syntax: note the lack of a $ symbol
 9 
10 echo "The school year is " . SCHOOL_YEAR;
11 
12 
13 
14 ?>

Standards

  • DefineGive the precise meaning of a word, phrase, concept or physical quantity. the terms: variable, constant, operator, object.

References