PHP

PHP Constants


 

In PHP, constants are identifiers for simple values that do not change during the execution of a script i.e. remains unchanged. Constant identifiers are always in uppercase by convention.

 

PHP constants can be declared in two ways:

  • Using difine() function
  • Using const keyword

 

1. Define Function:

The define() is a built in function which is used to define a constant.

 

Syntax:

<?php
    define("CONTSTANT_NAME", "constant_value");
?>

 

2. Const keyword:

The `const` keyword is an alternative way to define constants, which was introduced in PHP 5.3.

 

Syntax: 

<?php
    const CONSTANT_NAME = "constant_value";
?>

 

Example:

<?php
    define("PI", 3.14);
    const PIE = 3.14;
    echo PI;
    echo PIE;
?>

 

OUTPUT

 

Note: There is no need to use `$` in name convention and by default constants names are case-sensitive.