Define Constants in PHP | PHP Tutorial For Beginners

What is a Constant?

A constant is an identifier for a simple value. Once set, a constant's value cannot change during script execution. Constant names are case-sensitive and, by convention, always written in UPPERCASE. A constant name must begin with a letter or underscore (no $ sign).

How to Define a Constant

Use the define() function. To read the value, just use the constant's name — no $ prefix.

<?php
define("PAGE_TITLE", "PHP Tutorials for Beginners");
define("DESC",       "PHP constant tutorial example");

echo PAGE_TITLE; // Output: PHP Tutorials for Beginners
echo DESC;       // Output: PHP constant tutorial example
?>

Constants vs Variables

  • Constants have no $ sign — variables require it.
  • Constants are defined with define() — variables are assigned with =.
  • Constants are accessible anywhere in the script regardless of scope — variables are subject to scoping rules.
  • Once a constant is defined it cannot be redefined or undefined.

Hope this tutorial is useful for you. Keep following PHP Tutorial for Beginners for more help.

← Older Post Newer Post →