A constant in PHP is a fixed value that cannot be changed after it is created.
- Value cannot change
- A valid constant name starts with a letter or underscore $ sign can not use before constant define.
- Available globally
- Mostly written in CAPITAL letters
- Constants are case-sensitive by default.
How to Create Constants in PHP
1. Using define():
define(name, value, case-insensitive);- name: The name of the constant.
- value: The value of the constant.
- case-insensitive (optional): Indicates whether the constant name should be case-insensitive. Default is false.
<?php
define("SITE_NAME", "TutorialsonWeb");
echo SITE_NAME;
?>2. Using const:
<?php
const PI = 3.14;
echo PI;
?>| Variable | Constant |
Use $ sign | No $ sign |
| Value can change | Value cannot change |
Example: $name | Example: SITE_NAME |