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
Interview Questions & Answers
How to check if a variable is empty in PHP?
Is PHP a case sensitive language?
What are the different data types in PHP?
How to define a Constant in PHP?
What is the difference between $var and $$var in PHP?
Which special characters are allowed in PHP variable names?
Can we print multiple variables in a single echo statement in PHP?