Constants in PHP. Predefined PHP Constants

Lecture



PHP constants

There are cases when variables are rather inconvenient to use for permanent storage of any specific values ​​that do not change during the program operation. Such values ​​can be mathematical constants, paths to files, various passwords, etc. For this purpose, PHP provides for such a construction as constant .

Constant called a named quantity that does not change during the execution of the program (script).

Unlike variables, you cannot change the values ​​of the constants that were assigned to them when they were declared. Constants are useful for storing values ​​that should not be changed while the program is running. Constants can contain only scalar data (of logical, integer, floating, and string types).

In PHP, constants are defined by the define () function. This function has the following format:

define ( $name , $value , $case_sen ), где:

$name - имя константы;
$value - значение константы;
$case_sen - необязательный параметр логического типа,
указывающий, следует ли учитывать регистр букв (true) или нет (false).

An example of defining and using constants in PHP:

<? php
define ( "pi" , 3.14 , true );
echo pi ;
// Выводит 3.14
?>

If the $ case_sen parameter is true , the interpreter will be case sensitive when working with a constant. Note that constants are used without the leading $ sign.

Differences between constants and variables:

  • Constants do not have a prefix in the form of a dollar sign ( $ );

  • Constants can only be defined using the define () function, not by assigning a value;

  • Constants can be defined and accessed anywhere without regard to scope;

  • Constants cannot be defined or canceled after the initial declaration;

  • Constants can only have scalar values.

Check for the existence of constants

To test for the existence of a constant, you can use the defined () function. This function returns true if the constant is declared. Let's give an example:

<? php
// Объявляем константу pi
define ( "pi" , 3.14 , true );
if   ( defined ( "pi" )== true ) echo "Константа pi объявлена!" ;
// Скрипт выведет 'Константа pi объявлена!'
?>

PHP Predefined Constants

In PHP, there are the following predefined constants:

PHP provides a large list of predefined constants for each executed script. Many of these constants are defined by different modules and will only be present if these modules are available as a result of dynamic loading or as a result of static assembly.

There are five predefined constants that change their value depending on the context in which they are used. For example, the constant __LINE__ depends on the string in the script on which this constant is specified. Special constants are case insensitive and a list of them is given below:

Name
Description
__LINE__
The current line in the file.
__FILE__
The full path and name of the current file.
__DIR__
The directory of the file. The file is returned. This is equivalent to dirname (__ FILE__). This directory name doesn’t have a trailing slash unless it is the root directory.
__FUNCTION__
The name of the function. (Added in PHP 4.3.0.)
__CLASS__
Class name (Added in PHP 4.3.0.)
__TRAIT__
The trait name. It was declared in (eg Foo \ Bar).
__METHOD__
The name of the class method. (Added in PHP 5.0.0)
__NAMESPACE__
The name of the current namespace.
created: 2016-01-25
updated: 2021-03-13
132496



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)