php constants

Constants is a identifier that once defined in program or page they cannot be changed or undefined.
A constant is an identifier for a simple value. The value cannot be modified during the script’s execution. A valid constant name starts with a letter or underscore (don’t use dollar($) sign before the constant name) and by convention constant identifier are always uppercase.

Note: Constants are automatically global across the entire script.

Constants are defined by using the define() function or by using the const keyword outside a class definition as of PHP 5.3.0. The name of the constant and the value must be placed within the parentheses. After defining it can never be changed or undefined. Only scalar data i.e. boolean, integer, float and string can be contained in constants.

Syntax PHP Constant

define(name, value, case-insensitive)

define() function Parameters:
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false

The example below creates a simple constant with a case-sensitive name:

<?php
define("SIMPLE", "Hi, How Are You!");
echo SIMPLE;
?>

The example below creates a simple constant with a case-sensitive name:

<?php
define("SIMPLE", "Hi, How Are You!", true);
echo simple;
?>

Global Constants

You know constant automatically work as a global variable.

<?php
define("SIMPLE", "Hi, How Are You!");

function SimpleTest() {
    echo SIMPLE;
}
 
SimpleTest();
?>

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.