In this lesson, we will discuss PHP constants. What is PHP constant? How to create constant in PHP? What do PHP constants do? What are the PHP constant creation rules? Read on to find out.

Constants are simple data holders used to store data. Constants are like variables. Their only difference compared to variables is this: The data assigned to variables can be changed or removed later (made undefined or null); However, the data assigned to constants cannot be changed or removed later.

To create a constant, we use PHP's predefined define() function or the const keyword.

In the example below, we will define constants with the define() function and the const keyword.

define( 'CAR_NAME', 'MERCEDES' );
// define() fonksiyonuyla CAR_NAME adında bir sabit tanımladık ve MERCEDES değerini verdik.
echo CAR_NAME;
// Çıktı: MERCEDES
const COMPANY_NAME = 'Onur Özden Web Çözümleri';
// const anahtar kelimesiyle COMPANY_NAME adında bir sabit tanımladık ve Onur Özden Web Çözümleri değerini verdik.
echo COMPANY_NAME;
// Çıktı: Onur Özden Web Çözümleri

PHP Constant Creation Rules

  • Constant names can only start with a letter or _ (underscore).
  • (A-Z a-z 0-9) and _ (underscore) can be used in constant names.
  • Special characters can never be used in constant names.
  • When defining constant names, reserved words used by PHP (if, break, default, new, etc.) cannot be used.
  • The values ​​assigned to constants cannot be changed, removed or made undefined later.

PHP Constant Creation Recommendations and Tips

  • It is not recommended to write global code in constant names and to use Turkish characters for readability.
  • We can call the constant we created in the global area with the const expression in the local area. However, we cannot call the constant we created in the local area in the global area. This rule does not apply to constants we create with the define function. We can call the constants we created with the define function from any field to any field.
  • Created with the define() function, it is case sensitive by default. Optionally, constants created with the define() function can be case-insensitive by assigning false as the third argument to the function. Constants created with the const keyword are not case sensitive.
  • PHP currently supports both case-sensitive and case-insensitive constants. Case-insensitive constants are impractical, subject to various inconsistencies in functionality, and result in excessive implementation complexity. For this reason,  making constants case insensitive is never recommended.

PHP Constant Examples

In the example below, we will cover giving a value to a constant in the number data type and giving the value of another constant to the constant.

define('SOFTWARE_VERSION', 2.4);
// define() fonksiyonuyla SOFTWARE_VERSION adında bir sabit tanımladık ve 2.4 değerini verdik.
echo SOFTWARE_VERSION;
// Çıktı: 2.4
const VERSION = SOFTWARE_VERSION;
// const anahtar kelimesiyle VERSION adında bir sabit tanımladık ve SOFTWARE_VERSION sabitinin değerini verdik.
echo VERSION;
// Çıktı: 2.4

In the example below, we will handle assigning array type data to a constant.

define('SOFTWARE_VERSION', 2.4);
// define() fonksiyonuyla SOFTWARE_VERSION adında bir sabit tanımladık ve 2.4 değerini verdik.
echo SOFTWARE_VERSION;
// Çıktı: 2.4
const VERSION = SOFTWARE_VERSION;
// const anahtar kelimesiyle VERSION adında bir sabit tanımladık ve SOFTWARE_VERSION sabitinin değerini verdik.
echo VERSION;
// Çıktı: 2.4

In the example below, we will call the constant we created in the global area with the const keyword in the local area.

const GLOBAL_CONSTANT = 'CONSTANT VALUE';
// Global alanda const anahtar kelimesiyle GLOBAL_CONSTANT adlı bir sabit oluşuturduk ve sabite CONSTANT VALUE değerini verdik.
function simple_function(): string
{
    return GLOBAL_CONSTANT;
}

// simple_function adlı bir fonksiyon oluşuturduk ve sabiti döndürdük (return).
echo simple_function();
// Döndürdüğümüz sabiti yazdırdık
// Çıktı: CONSTANT VALUE

In the example below, we will call the constant we created in the local area with the const keyword in the global area. Note: This example returns error output. Because this usage is incorrect.

// Local alanda const anahtar kelimesiyle GLOBAL_CONSTANT adlı bir sabit oluşuturduk ve sabite CONSTANT VALUE değerini verdik.
function simple_function(): string {
	const GLOBAL_CONSTANT = 'CONSTANT VALUE';
	return GLOBAL_CONSTANT;
}
// simple_function adlı bir fonksiyon oluşuturduk ve sabiti döndürdük (return).
echo simple_function();
// Döndürdüğümüz sabiti yazdırdık
// Çıktı: Parse error: syntax error, unexpected 'const'  (T_CONST)
// Bu hata çıktısını almamızın nedeni local alanda const anahtar kelimesiyle sabit oluşturamamızdır. Const anahtar kelimesiyle bir sabiti sadece top-level veya global dediğimiz tüm scriptte geçerli olan alanlarda oluşturabiliriz.