🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

Ways to Define PHP Constant and Use It in Your Code

Reading time 3 min
Published Aug 8, 2017
Updated Oct 2, 2019

PHP constants are comparable to the simpler types of data variables (strings, floats, booleans, and integers) as they can only store scalar data. As the name itself suggests, the value of every PHP constant is constant: it cannot be modified.

While variables didn't require any special creation process, constants don't work if a developer hasn't defined them. You have to make PHP define constants using a define() function. In this simple tutorial, we will review all the other ways in which PHP constants are different from variables, and learn to handle and use them.

PHP Constant: Main Tips

  • PHP Constants are very similar to variables, however, they cannot be changed or redefined after being once created and assigned a value.
  • Constants cannot exist undefined. To PHP define constant, remember not to use $ symbol, as you would for a variable.
  • While variables can be local or global, constants are universally global and can be used in the entire script, inside or outside a function.

define() Function Explained

To create a PHP constant, you have to use the function called define(). You will have to declare its name and value, which you won't be able to change later in time.

A name of a PHP constant has to abide by the same rules as the name of a variable. It must begin with a letter or underscore (_) but can contain as many letters or numbers as you like. The value it holds has to be scalar data (it cannot, for example, be used to store PHP arrays).

  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

Standard Syntax Revealed

The following example reveals how it should look in your script when you PHP define constant before using it for the first time:
define(name, value, case-insensitive)

  define("GreetUser", "Welcome to BitDegree Learn!");
  function my_test() {    
    echo GreetUser;

Each define() parameter explained:

  • name: we define the name of the PHP constant, following the rules above.
  • value: we declare the value (scalar data only) of the constant.
  • case-insensitive: we choose (True/False) whether a constant PHP script is case-insensitive. If we skip it, the constant is case-sensitive by default.

Look at the example in which we will PHP define constant. You will notice the last parameter is skipped, therefore the result is a case-sensitive PHP constant name:

  define("GreetUser", "Welcome to BitDegree Learn!");
  echo GreetUser;

Now let's try to make a case-insensitive name. It's simple: all we need to do is set the last parameter to true, thus declaring the name is indeed case-insensitive:

  define("GreetUser", "Welcome to BitDegree Learn!", true);
  echo greetuser;

PHP Constant: Summary

  • Constants and variables have their similarities, but you cannot change or redefine a constant after it's created. Names of constants also don't include $ symbol.
  • Constants must be defined: cannot exist undefined.
  • Unlike variables, constants are always global and can be used anywhere in the code.