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).

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)

Example
<?php
  define("GreetUser", "Welcome to BitDegree Learn!");
  function my_test() {    
    echo GreetUser;
  }
  my_test();  	
?>

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:

Example
<?php	
  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:

Example
<?php  	
  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.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()