How to define a Constant in PHP?
Use the define() function or the const keyword. A constant is like a variable, but its value can not change after it is set. It is used for values that rarely change, like a tax rate or the site name.
<?php
define("TAX_RATE", 18);
const SITE_NAME = "Tutorials Class";
echo SITE_NAME . " tax: " . TAX_RATE . "%";
?>
Output:
Tutorials Class - Output Window
Tutorials Class tax: 18%
Do not write the dollar sign ($) before a constant name. By convention names are in capital letters. Read all the rules in PHP Constants, and see the constant() function.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Variables |
| PHP Data Types |
| PHP Constants |
| PHP isset() Function |
| PHP empty() Function |
| PHP is_null() Function |
| Exercises & Assignments |
|---|
| Write a program to print “Hello PHP” using variable |
| Write a program to print a string using echo+variable. |
| Write a program to print two variables in single echo |