The PHP constant expression is a name or an identifier which will never change its values during the script execution. The PHP constant can be defined with the predefined PHP function called – ”define()”.

There are some points that have to be considered during writing the PHP constants.

  • The PHP constant should be written in uppercase.
  • If you assigned a value to the constant, you shouldn’t declare or change it in the following lines of the PHP source code.
  • The constant should start with letters, so the numerals are not accepted, especially in the beginning.
  • The constant wouldn’t start with a dollar sign ($).
  • It can start with a double underscore, but you have to make sure that you already don’t use the predefined magic constant name.

Anyway, let’s take a look at the – ”define()” predefined PHP function.

Understanding the DEFINE PHP Function and CONST PHP Keyword

The PHP “define” callback is a predefined function used to define a new constant in the PHP language. This callback takes 3 parameters.

  • The constant identifier with a valid name.
  • The constant value.
  • The case-insensitive, which takes a boolean value, refers to whether you can call it with a lower case or not. The default value is false.
<?php 
  define( "CONSTANT_NAME", "Constant Value", $case_insensitive );
?>

Let’s take an example.

<?php 
  define( "SITENAME", "CodedTag.com");
  echo SITENAME;
  echo "\n";
  echo sitename;
?>

The output would be like the following.

CodedTag.com
sitename

The first value is printed correctly, but the second one printed another value, the name of constant, and that happened because I didn’t specify the last parameter that called case-insensitive.

Let’s specify the last parameter to see the result.

<?php 
  define( "SITENAME", "CodedTag.com", true );
  echo SITENAME;
  echo "\n";
  echo sitename;
?>

The output would be like the below

CodedTag.com
CodedTag.com

So if you like to use lowercase during the code, you have to specify the last parameter and set it with a “true” value.

Since PHP 7 was released, it allows you to use the array with the PHP constant.

<?php 
  define( "USERNAMES", array(
     "Peter",
     "Ahmed",
     "Michael",
     "Montasser"
  ));
  print_r(USERNAMES);
?>

The output would be like the below one.

Array ( [0] => Peter [1] => Ahmed [2] => Michael [3] => Montasser )

Also, the PHP constant can be used as a global inside the PHP function.

<?php 
  define( "SITENAME", "CodedTag.com");
  function get_sitename() {
    echo SITENAME;
  }  
  get_sitename(); // CodedTag.com
?>

The PHP constant can be defined with another predefined word called const. Check the following example.

<?php 
  const DATASOURCE = "DataSource";
  echo DATASOURCE; // DataSource
?>

But what is the difference between the “define” function and the keyword “const”? In the following section, I will answer you with a poof. Let’s dive right in.

The Difference between the CONST Keyword and the DEFINE Function

The two both are PHP constants, each one does the same job. Check the below code.

<?php
  const USER = "Montasser";
  define( "USER", "Montasser" );
?>

So the two both are the same. But the first one, “const” is defining the value during the translation. On the other hand, the “define()” function is defining the value at the execution time.

The “define()” function can be used as a global, so you can place it inside the if condition. But the “const” only appears on the scope position.

So we can use the “define()” function in the condition block.

<?php 
  if ( !defined( "VERSION" ) ) {
    define( "VERSION", "v11.9" );
  }
?>

But you will never be able to do that with the keyword “const”. Because it only works in the scope position.

In the “define()” function, you were not able to use the array inside in PHP 7 prior. But the “const” keyword is already working with the array since PHP 5.6. To learn more details, visit the PHP manual.

Anyway, the PHP constant is working with PHP Data Types except for some types like iterable, resource, and so on. In the following sections, I will show you the constant with each type wrapped with an example.

Use the PHP Constant with the PHP Scalar Types

In previous sections, I explained how to use the constant with a string data type, and here I will cover the other scalar data type such as integers, floats, and boolean.

Use the constant with the integer data type.

<?php 
  define( "CAR_SPEED", 120 ); 
  echo CAR_SPEED; // 120
?>

The float data type with constant.

<?php 
  define( "WEIGHT", 25.65 ); 
  echo WEIGHT; // 25.65
?>

Use the boolean data type with the constant.

<?php 
  define( "IS_DISABLED", true ); 
  echo IS_DISABLED; // 1
?>

Use the PHP Constant Expression with the PHP Compound Types

As I mentioned before, since PHP 7 was released, you can use the array data type with the constant. But how it does work with the PHP object?

Actually, the PHP object will not work with the constant because the class object can be changed during the execution. That means the PHP object is changeful which is similar to the variable.

Use the Constant with the Special Types

In the special types, there are two data types, null, and resource. And here, the PHP constant only supports the null data type.

<?php 
define( "NO_VALUE", NULL ); 
  echo gettype(NO_VALUE); // NULL
?>

The PHP resource is not supported with PHP constant expression because the values inside are changeable. So it will not work.

Wrapping Up

In this tutorial, you understood the constant with examples. And that was through the “define()” function and the “const” keyword.