Constants in PHP

This tutorial explains how to define and use constants in PHP. Constants contain a fixed value that cannot be changed after it has been set. Constants are useful for when you want to store data that you need to protect from accidentally being altered.

Constant names follow the same rules as variable names except they don’t start with a $ dollar sign. However, constant names are usually capitalised to distinguish them from variable names. Constants must also be defined using the define() function. Constants can contain alphanumeric values (like strings, integers, and floating point numbers) but can’t store Boolean values or represent a NULL value of an object.

Constants cannot be included in mixed strings when being displayed and must be concatenated instead. Watch the video below to find out how to define and use constants and then scroll down to see the sample code.

Sample PHP code:

<?php
  define('USERNAME','batman');
  echo 'Hello ' . USERNAME;
?>