A constant is a name or an identifier for a single value. The value of the constant cannot be changed during script execution. By default, a constant is case sensitive. By convention, constant identifiers are always uppercase. The constant name begins with a letter or underscore (_), followed by any number of letters, numbers or underscores. If you have defined a constant, it may never be changed or redefined.

To define a constant in PHP, you must use the define () function and to get the value of a constant, you must specify its name. Unlike variables, you don't need to add $ to constants. You can also use the constant () function to read a constant value if you want to dynamically get the name of the constant.

The constant () function in PHP

As the function name mentioned, this function will return the value of the constant.

This is very useful when you want to get the value of a constant, but you don't know its name, for example, it is stored in a variable or returned by a function.

For example, the constant () function in PHP

 

Only scalar data (Boolean, integer, float and string) can be kept in constants.

The difference between constants and variables in PHP is:

To define variables, write the character $ first, otherwise there is no need.

Constants cannot be defined by simple assignments, they can only be defined using the function define ().

Hangs can be defined and accessed anywhere without regard to variable scope rules.

A constant when defined, it cannot redefine or cancel the definition.

Valid and invalid constant names in PHP

 // Vi du dang hang hop hop 
define ("ONE", "Vi khong hang no 1");
define ("TWO2", "Vi du dang hang noi le 2");
define ("THREE_3", "travel to the cave without 3")

// Vi du dang hang hop hop
define ("2TWO", "Vi khong hang no 1");
define ("__ THREE__", "Vi khong hang no 2)";

Magic Constant in PHP

PHP provides a large number of predefined constants so that any script can use it.

There are 5 magic constants, depending on where they are used. For example, the value of __LINE__ depends on the line it is used in your script. These special constants are case-sensitive.

The following table lists some magic constants in PHP:

Name Description __LINE__ Current line of file __FILE__ Full path and full file name. If used within an include, the name of the included file will be returned. Since PHP 4.0.2, __FILE__ always has an absolute path, while in older versions they contain relative paths in some cases __FUNCTION__ Name of function. (Added in PHP 4.3.0) As in PHP 5, this constant returns the name of the function as it was previously declared (case sensitive). In PHP 4, its value is always lowercase __CLASS__ Name of the class. (Added in PHP 4.3.0) As in PHP 5, this constant returns the name of the class as it was previously declared (case sensitive). In PHP 4 its value is always lowercase __METHOD__ Class method name. (Added in PHP 5.0.0) This method name is returned as previously declared (case sensitive).

Follow tutorialspoint

Last lesson: What is PHP?

Next lesson: Operator in PHP

5 ★ | 1 Vote | 👨 202 Views

Above is an article about: "Constants in PHP". Hope this article is useful to you. Don't forget to rate the article, like and share this article with your friends and relatives. Good luck!

« PREV POST
NEXT POST »