7/17/25, 4:50 PM PHP Constants
Tutorials Exercises Services Get Certified
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
PHP Constants
❮ Previous Next ❯
Constants are like variables, except that once they are defined they cannot be changed or
undefined.
PHP Constants
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).
Note: Unlike variables, constants are automatically global across the entire script.
Create a PHP Constant
To create a constant, use the define() function.
Syntax
define(name, value);
Parameters:
[Link] 1/9
7/17/25, 4:50 PM PHP Constants
name: Specifies the name of the constant
Tutorials
value: the
Specifies Exercises Services
value ofthe constant Get Certified
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Example Get your own PHP Server
Create a constant with a case-sensitive name:
define("GREETING", "Welcome to [Link]!");
echo GREETING;
Try it Yourself »
ADVERTISEMENT
PHP const Keyword
You can also create a constant by using the const keyword.
Example
Create a case-sensitive constant with the const keyword:
[Link] 2/9
7/17/25, 4:50 PM PHP Constants
const MYCAR = "Volvo";
Tutorials
echo MYCAR;
Exercises Services Get Certified
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Try it Yourself »
const vs. define()
const cannot be created inside another block scope, like inside a function or inside an if
statement.
define can be created inside another block scope.
PHP Constant Arrays
From PHP7, you can create an Array constant using the define() function.
Example
Create an Array constant:
define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0];
Try it Yourself »
Constants are Global
[Link] 3/9
7/17/25, 4:50 PM PHP Constants
Constants are automatically global and can be used across the entire script.
Tutorials Exercises Services Get Certified
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Example
This example uses a constant inside a function, even if it is defined outside the function:
define("GREETING", "Welcome to [Link]!");
function myTest() {
echo GREETING;
}
myTest();
Try it Yourself »
?
Exercise
What is a correct syntax to define a constant?
constant("MESSAGE", "Welcome to my home");
constant "MESSAGE" = "Welcome to my home";
define("MESSAGE", "Welcome to my home");
Submit Answer »
❮ Previous Next ❯
[Link] 4/9
7/17/25, 4:50 PM PHP Constants
Tutorials Exercises Services Get Certified
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
ADVERTISEMENT
[Link] 5/9
7/17/25, 4:50 PM PHP Constants
Tutorials Exercises Services Get Certified
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
COLOR PICKER
ADVERTISEMENT
[Link] 6/9
7/17/25, 4:50 PM PHP Constants
Tutorials Exercises Services Get Certified
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
ADVERTISEMENT
ADVERTISEMENT
[Link] 7/9
7/17/25, 4:50 PM PHP Constants
Tutorials Exercises Services Get Certified
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
PLUS SPACES
GET CERTIFIED FOR TEACHERS
FOR BUSINESS CONTACT US
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
[Link] 8/9
7/17/25, 4:50 PM PHP Constants
Python Reference
Tutorials [Link] Reference
Exercises
Bootstrap Reference
Services Get Certified
PHP Reference
HTML
CSS HTML ColorsSQL
JAVASCRIPT PYTHON JAVA PHP HOW TO [Link] C
Java Reference
Angular Reference
jQuery Reference
Top Examples Get Certified
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate
FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and
privacy policy.
Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].
[Link] 9/9