by Dinesh Thakur

In programming, PHP allows you to define constants, it is often necessary to define data structures whose value must not change during program execution. These data structures are typically what is known constants. The most famous mathematical constants is for instance the PI number whose value is approximately 3.1415926535898. Note that PHP natively integrates the M_PI constant. This can then be used for any calculation of circumference.

The constants declare by the define () statement.
So,
define ("Country", "India");
define ("Code",91 );
creates two constants, and Country Code, which can be used very naturally;
echo Country." ".Code;

For example. Begin by studying how constant is declared in a PHP program.

Creating and playing constants

<?php
   define ("PI",3.1415926535, TRUE);
   echo "The PI constant is ", PI, "<br />";
   echo "The PI is constant ", pi, "<br />";
   // Verification of existence
   if (defined ("PI")) echo "The PI constant is already defined", "<br />";
   if (defined ("pi")) echo "The constant pi is already defined", "<br />";
   // set sensitive, verification of the existence and use
   if (define ("site", "http://.ecomputernotes.com", FALSE))
      {
           echo "<a href=\"",site, "\"> Link to my site </ a>";
      }
?>

The native function define ()

The constant declaration is prepared using the native function define () which takes three parameters, the first two are required. Here are the details of the official documentation of the define () function:

Prototype define () function

boolean define (string $name, mixed $value, [boolean $case_insensitive])

• The first parameter of the function is a string that defines the name of the constant. By convention, all constants must be written in capital letters.
• The second is the value that is assigned to that constant. This may be a string, an integer, a real or even a Boolean.
• The third argument is optional. This is a boolean indicating whether the interpreter should be concerned with the case or not constant.

We advise never to complete this third argument to impose a rigorous writing code. The first two obligatory enough. Turning to the statement of our first constant.

Declaration of a Constant

As everyone knows (or almost), water boils at a temperature of 100 ° C (in theory). We will declare a first constant BOILING_WATER_TEMPERATURE name and contains a numerical value information 100.

Declaration of a Constant

<?php
       // Declare the constant
      define ('BOILING_WATER_TEMPERATURE', 100);
      // Display the value
      echo 'Water boils at ' , BOILING_WATER_TEMPERATURE , ' °C ';
?>

If we test this script, we see that the string "Water boils at 100°C" is displayed correctly on the screen, which proves that the constant has been declared and initialized to the correct value .

Notes:

[1] In the statement, if the value is a number, it is registered as the function.
[2] To read the value of a constant, simply call him by name.

From a chemical point of view, water is also a molecule consisting of two hydrogen atoms which are associated with an oxygen atom. Its chemical formula is thus written H2O. We will therefore create a new constant name FORMULA_WATER H2O representing a value chain of characters.

Declaring a new constant

<?php
     // Declare the constant
     define ('FORMULA_WATER', 'H2O');
     // Display the value
    echo 'Chemical formula\'s of water : ', FORMULA_WATER;
?>

Note: the statement, if the value is a string, you must surround it with single or double quotes.

Redefinition of a constant

The value of a constant can not be redefined! Syntax errors will be returned in case of constant redefinition attempt or assigning a new value. The following two scripts have these two cases respectively.

Errors generated in the event of redefinition of a constant

•[Generated error] Notice: Constant BOILING_WATER_TEMPERATURE already defined in /Applications/MAMP/htdocs/PHP-Code/constant.php on line 6
•[Generated error] Parse error: syntax error, unexpected '=' in /Applications/MAMP/htdocs/PHP-Code/xml.php on line 6

Note the = sign for assigning a value to a variable, not a constant.

The predefined constants

It exists in many PHP predefined constants, which can include used in functions as parameters for defining options. We can not mention them all as they are numerous, but we will define them As our needs.
The table defines some useful constants to know. If, out of curiosity, you want view all existing constants, you can write the following code:

<?php
   print_r (get_defined_constants ());
?>


You get an impressive list, including many of the values are integers.

Some predefined constants

PHP_VERSION PHP version installed on server
PHP_OS Name of the server operating system
DEFAULT_INCLUDE_PATH Path to default file
__FILE__ Name of running file
__LINE__ Line number running