PHP Constants

Values that never change.

✍️ 2 exercises in this lesson

A constant is a name for a value that cannot change during the script.

  • define("SITE_NAME", "CodesInTech");
  • const TAX_RATE = 0.18;
  • Constants have no $ and are global automatically.
  • By convention, constant names are UPPERCASE.

PHP also has "magic constants" like __LINE__, __FILE__ and __DIR__.

Key Takeaways: PHP Constants

Mastering PHP Constants helps you write clean and reliable PHP code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

PHP Constants Code Example

PHP
<?php
define("SITE_NAME", "CodesInTech");
const TAX_RATE = 0.18;

$price = 1000;
echo SITE_NAME . "<br>";
echo "Total with tax: " . $price * (1 + TAX_RATE) . "<br>";
echo "This is line " . __LINE__;
Output
CodesInTech
Total with tax: 1180
This is line 8
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

PHP Constants Practice Exercises

Check what you learned in “PHP Constants”.

/ 2 solved
Fill in the blank

Define a constant.

("SITE_NAME", "CodesInTech");
✓ Correct! Not quite. Try again.
Multiple choice

Do constants start with $?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services