PHP Constants
PHP Tutorial » PHP Constants
What is a PHP constant?
PHP constants are names or identifiers that cannot be changed during script execution
Constants are similar to the variable, except that, once defined, they can never be undefined or modified. These remain constant throughout the program.
Constants does not need $ in front.
But, as a convention of PHP programmers, constants are written in capital letters (A-Z) for easier observation.
To create a constant, use the define() function.
Syntax:
define(name, value, case-insensitive)}
?>
Where:
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive:
Constant with a case-insensitive name example
to create a constant with a case-sensitive name:
<?phpdefine("MESSAGES", "Welcome to agernic.com, have a nice day.");
?>
echo MESSAGES;
Create a constant with a case-insensitive name:
<?phpdefine("MESSAGES", "Welcome to agernic.com, have a nice day.", true);
?>
echo messages;
Related subjects:
PHP foreach
What is an array
Convert array to string comma
Array to comma separated
Tags: php constant array , php constant object, php define constant, php constant variable in class, php const vs define, php 7 constants, use php constant in javascript,
php constant in string,
What is a PHP constant?
How do you call a constant in PHP?
Where do I put constants in PHP?
What is PHP magic constant?
PHP Constant Arrays
In PHP7, we can create an Array constant using function define().
<?php
define("colors", [
"red",
"green",
"blue"
]);
echo colors[2];
?>
Constants are Global
PHP constants are automatically global and can be used throughout the script.
<?php
define("MESSAGES", "Welcome to agernic.com, have a nice day.");
function my_Test() {
echo MESSAGES;
}
my_Test();
?>
php constant array , php constant object, php define constant, php constant variable in class, php const vs define, php 7 constants, use php constant in javascript,
php constant in string,
What is a PHP constant?
How do you call a constant in PHP?
Where do I put constants in PHP?
What is PHP magic constant?
How do you call a constant in PHP? - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.