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 Constants
Exercises & Assignments
No Content Found.
Interview Questions & Answers
How to define a Constant in PHP?