PHP স্থায়ী

Constants are similar to variables, but once a constant is defined, it cannot be changed or undefined.

PHP স্থায়ী

Constants are identifiers for single values (names). The value cannot be changed within the script.

Valid constant names start with a letter or underscore (there is no $ symbol before the constant name).

Note:Unlike variables, constants are automatically global throughout the script.

Set PHP Constant

To set a constant, use define() Function - It uses three parameters:

  1. The first parameter defines the name of the constant
  2. The second parameter defines the value of the constant
  3. An optional third parameter specifies whether the constant name is case-insensitive. The default is false.

The following example creates aCase-sensitive ConstantsWith the value of "Welcome to codew3c.com!":

Example

<?php
define("GREETING", "Welcome to codew3c.com!");
echo GREETING;
?>

Run Instance

The following example creates aCase-insensitive ConstantsWith the value of "Welcome to codew3c.com!":

Example

<?php
define("GREETING", "Welcome to codew3c.com!", true);
echo greeting;
?>

Run Instance

Constants are global

Constants are automatically global and can be used throughout the entire script.

The following example uses a constant within a function, even if it is defined outside the function:

Example

<?php
define("GREETING", "Welcome to codew3c.com!");
function myTest() {
    echo GREETING;
}
myTest();
?>

Run Instance