HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Constants are like variables except that once they are defined they cannot be changed or undefined
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
A valid constant name starts with a letter or underscore (no $ sign before the constant name).
To create a constant, use the define()
function.
Syntax
Parameters:
Create a constant with a case-sensitive name:
<?php
define("GREETING", "Welcome to W3C!");
echo GREETING;
?>
Create a constant with a case-insensitive name:
<?php
define("GREETING", "Welcome to W3C!", true);
echo greeting;
?>
In PHP7, you can create an Array constant using the define()
function.
Create an Array constant:
<?php
define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0];
?>
Constants are automatically global and can be used across the entire script.
This example uses a constant inside a function, even if it is defined outside the function:
<?php
define("GREETING", "Welcome to W3C!");
function myTest() {
echo GREETING;
}
myTest();
?>