Constants in PHP: Complete Guide to Understand PHP Constants

What are the constants in PHP?

PHP Constants are variables whose values ​​cannot be changed once defined. These constants are defined without the $ sign at the beginning.

PHP constants are defined using the “define()” function or “const()” keyword.

Example using define() function:

define("CODE", "errors");

Example using const() keyword:

const CODE = "errors";

This function accepts two parameters: the first is the name, and the second is the value of the defined constant.

There are two ways to define a constant in PHP:

  • The first method is to use the define() function and it is processed at runtime.
  • The second method is to use the “const” keyword. In PHP is defined using this set during compile time before the code is executed.

How to Create Constants in PHP?

To create a constant in PHP, you can use the define() function, and the constant as the name suggests, hold values that cannot be changed during the execution of the script.

In this section, we will try to create constants in PHP that will allow you to understand the implementation.

Syntax

PHP constant: define()

define(name, value, case_insensitive)

A simple reminder: Ensure you properly follow consistent syntax delegation in your projects.

This helps fulfill the project’s desired function and avoids unwanted errors and bugs.

Parameters

These are the following functions to implement the parameters of constant.

  • name: This is the name of the constant you want to define. It’s a string that serves as an identifier for the constant.
  • value: This is the value that you want to assign to the constant. It can be of any data type: scalar, array, or resource.
  • case-insensitive: This is an optional parameter. If set to “true“, it makes the constant name case-insensitive. By default, constant names are case-sensitive.

Note that you can make the program case-insensitive by changing the default value to true.

Additionally, the name and value parameters were required from the start.

A case-sensitive parameter is automatically set to false even if it is not mentioned in the function.

Example Code of Constants in PHP with Case-sensitive

Here’s an example code demonstrating the use of constants in PHP with case-sensitive:

<?php
// Define constants with case sensitivity
define("HELLO", "Welcome to Source Code Hero", false); //case-sensitive

echo  HELLO;
?>

Output:

Welcome to Source Code Hero

  • The first parameter “HELLO” is the name of the constant. Constants in PHP are typically written in uppercase letters.
  • The second parameter “Welcome to Source Code Hero” is the value assigned to the constant HELLO.
  • The third parameter “false” indicates that the constant is case-sensitive.
  • Note that in modern PHP versions, the case-sensitivity declaration is ignored, as indicated by the warning message you received.

 Example Code of Constants in PHP with Case-insensitive

Here’s an example code demonstrating the use of constants in PHP with case-insensitive:

<?php
// Define constants with case sensitivity
define("HELLO", "Welcome to Source Code Hero", true); //case-insensitive

echo  HELLO;
?>

Output:

Warning: define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported in C:\xampp\htdocs\Sample\index.php on line 3
Welcome to Source Code Hero

  • The first parameter “HELLO” is the name of the constant. Constants in PHP are typically written in uppercase letters.
  • The second parameter “Welcome to Source Code Hero” is the value assigned to the constant HELLO.
  • The third parameter true indicates that the constant is case-insensitive.
  • Note that in modern PHP versions, the case-insensitivity declaration is ignored, as indicated by the warning message you received.

Difference Between Constant and Variable in PHP

ConstantVariable
1. A constant in PHP is a name or an identifier for a simple value.1. A variable in PHP is a container used to store data temporarily during the execution of a script.
2. Once defined, its value cannot be changed during the execution of the script.2. The value of a variable can change throughout the execution of the script.
3. Constants are defined using the define() function and typically written in uppercase letters.3. Variables are declared using the dollar sign ($) followed by the variable name.
4. Constants are global in scope, meaning they can be accessed from anywhere within the script.4. Variables can have different scopes: local, global, or static.
5. Constants are defined using the define() function and are typically written in uppercase letters.5. Local variables are accessible only within the function or block of code where they are declared.
6. Local variables are accessible only within the function or block of code where they are declared.
7. Static variables retain their value between function calls.
8. Variables are commonly used for storing data that may change during the execution of the script, such as user input, calculations, or database query results.

How to create an Array Constant using the define() function?

Here’s a basic example of creating an Array constant using the “define()” function:

<?php
  // The constant - using 'define' construct
define('PROGLANG', [
	 'JAVA',
	  'KOTLIN',
	 'PHP',
	  'PYTHON'
]);


echo PROGLANG['0'];
?>

Output:

JAVA

This PHP code defines a constant named PROGLANG using the define() construct. 

The constant is assigned an array containing four programming language strings: ‘JAVA’, ‘KOTLIN‘, ‘PHP’, and ‘PYTHON‘.

Then, the code attempts to echo/print the element at index ‘0’ of the PROGLANG constant, which should output ‘JAVA‘.

Understanding Constant keyword

There are two types of constants in PHP: constants and class constants.

Constants can be defined almost anywhere with a defining construct, while class constants are defined within a single class or interface with the const keyword.

While we can’t say that one constant type is more important than the other, PHP 5.6 introduced a difference between the two by allowing class constants with an array data type.

In addition to this difference, both constant types support scalar values ​​(integer, floating point, string, boolean, or zero).

PHP version 7 fixes this inequality by also adding an array data type to the constants, creating valid expressions:

 <?php
// The class constant - using 'const' keyword
class Category {
  const FOOD = [
    'Ice cream',
    'Burger',
    'Fries',
     'Bread'
  ];
}

// The class constant - using 'const' keyword
interface Games {
  const ESPORT = [
    'Wild Rift',
    'Mobile Legend',
    'CSGO 2',
     'DOTA 2'
  ];
}


echo Category::FOOD['2'];
echo "</br>";
echo Games::ESPORT['1'];
?>

Output:

Fries
Mobile Legend

Frequently Asked Questions (FAQs)

What is constant in PHP?

PHP Constants are variables whose values ​​cannot be changed once defined. 

How to Define Constant in PHP?

PHP constants are defined using the “define()” function or “const()” keyword.

How to Create a Constant by using the const Keyword?

Here’s an Example PHP code using the const keyword:

How to Create a Constant by using the define() function?


Here’s an Example PHP code using the const keyword:

Summary

In summary, PHP constants mean values ​​that cannot be changed.

This means that when a programmer continuously applies constants to a program, their value remains the same throughout program execution.

Additionally, the advantage of using the same value throughout the project is that their application is global.

You can refer to the examples above and try them yourself to demonstrate how they work.

Leave a Comment