Constants and Literals


Constants and Literals

A constant is a fundamental value's name or identifier. During the execution of the script, a constant value cannot change. A constant is case-sensitive by default, and constant identifiers are always in uppercase by convention. A constant name begins with a letter or underscore and continues with any number of letters, digits, or underscores. The same can never be changed or explained once it has been defined.

You must use the define() method to define a constant, and you merely give its name to obtain its value. You don't need a constant with a $ as you do with variables. If you want to get the name of a constant dynamically, you may use the function constant() to retrieve its value.

constant() function

The function's name implies that it will return the value of the constant.

This comes in handy when you need to obtain the value of a constant but don't know its name, such as when it's stored in a variable or returned by a function.

constant() example

<?php define("MINSIZE", 60); echo MINSIZE; echo "<br>"; echo constant("MINSIZE"); // same thing as the previous line ?>

output:

60 60

Constants can only hold scalar data (boolean, integer, float, and string).

Differences between constants and variables are

  • A constant does not require using a dollar sign ($), whereas a Variable involves using a dollar sign ($).
  • Simple assignment cannot define constants; instead, the define() function must be used.
  • Constants can be declared and referenced everywhere without respect to variable scoping rules.
  • Constants cannot be redefined or undefined after they have been set.

Valid and invalid constant names

// Valid constant names define("ONE", "first thing"); define("TWO2", "second thing"); define("THREE_3", "third thing"); define("__THREE__", "third value"); // Invalid constant names define("2TWO", "second thing");

PHP Magic constants

Any script that runs on PHP has access to many predefined constants.

Five magical constants vary depending on their application. The value of __LINE__, for example, is determined by the line on which it is used in your script. The following are the case-insensitive special constants:

Below are a few "magical" PHP constants:

Sr.No Name & Description
1 __LINE__
The file's current line number.
2 __FILE__
The file's entire location and filename. If used within the merge, the inserted file name is restored. Since PHP 4.0.2, __FILE__ has always included an absolute path, although in previous versions it occasionally contained a relative path.
3 __FUNCTION__
PHP 4.3.0 introduced this feature.) As of PHP 5, this constant returns the function name as it was declared (case-sensitive)
4 __CLASS__
The name of the class. (This was added in PHP 4.3.0) This constant, as of PHP 5, returns the class name as it was declared (case-sensitive). Its value is always lowercase in PHP 4.
5 __METHOD__
The name of the class method. (This was added in PHP 5.0.0.) The name of the method is returned exactly as it was stated (case-sensitive).