WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Constants


Once the constant value is defined, it cannot be changed anywhere else in the script.


PHP constants

Constant is a simple value identifier. This value cannot be changed in the script.

A constant consists of English letters, underscores, and numbers, but numbers cannot appear as the first letter. (The constant name does not need the $ modifier).

Note: Constants can be used throughout the script.


Set PHP constants

To set constants, use the define () function. The function syntax is as follows:

Syntax

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )

The function has three parameters:

  • name: Mandatory parameter, constant name, ie identifier.
  • value: Mandatory parameter, constant value.
  • case_insensitive : Optional parameter. If set to TRUE, this constant is case-insensitive. The default is case sensitive.

In the following example, we create a case sensitive constant , the constant value is "Welcome to welookups.com":

Example

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

The example below creates a constant with a case-insensitive name:

Example

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

Constants are Global

Constants are defined as global variables by default and can be used anywhere in the entire running script.

The following example demonstrates the use of constants inside a function, even if the constant is defined outside the function, the constant can be used normally.

Example

<?php
define("GREETING", "Welcome to welookups.com!");

function myTest() {
    echo GREETING;
}
 
myTest();
?>