PHP Constants

PHP Constants are global variables whose value cannot be changed during execution and can only store scalar values.

Scalar values are also called primitive values, which includes: string, number, boolean, null.

Constants are defined using the define($name, $value) function. 

PHP Constants Example

  Do It Yourself

PHP Constants Naming Convension

Unlike variables, PHP Constants do not need to start with the dollar sign($). Constants once defined cannot be changed during compile time. Constants are widely written in uppercase and must start with either a letter or an underscore.

PHP Constants Naming Convention Example

  Do It Yourself

PHP defined() Function

defined(string $constantName):bool is a function that checks if a constant exists. It only applies to constants unlike isset() which can be used to check if a variable exists It returns true if a constant is already defined or false if it does not.

PHP Constants defined() Function

  Do It Yourself

What You Should Know at the End of This Lesson

  • You should know how to define a PHP constant.
  • You should how the PHP constant naming convention works.
  • You should know how to check if a constant exists with defined().