PHP Constants (original) (raw)

Summary: in this tutorial, you learn about PHP constants and how to use the define() function and const keyword to define constants.

Introduction to PHP constants #

A constant is simply a name that holds a single value. As its name implies, the value of a constant cannot be changed during the execution of the PHP script.

To define a constant, you use the define() function. The define() function takes the constant’s name as the first argument and the constant value as the second. For example:

`<?php

define('WIDTH','1140px'); echo WIDTH;`Code language: PHP (php)

Try it

By convention, constant names are uppercase. Unlike a variable, the constant name doesn’t start with the dollar sign($).

By default, constant names are case-sensitive. It means that WIDTH and width are different constants.

It’s possible to define case-insensitive constants. However, it’s been deprecated since PHP 7.3

In PHP 5, a constant can hold a simple value like a number, a string, or a boolean value. From PHP 7.0, a constant can hold an array. For example:

`<?php

define( 'ORIGIN', [0, 0] );`Code language: PHP (php)

Try it

Like superglobal variables, you can access constants from anywhere in the script.

The const keyword #

PHP provides you with another way to define a constant via the const keyword. Here’s the syntax:

const CONSTANT_NAME = value;Code language: PHP (php)

In this syntax, you define the constant name after the const keyword. You use the assignment operator (=) and the constant value to assign a value to a constant. The constant value can be scalar, e.g., a number, a string, or an array.

The following example uses the const keyword to define the SALES_TAX constant:

`<?php

const SALES_TAX = 0.085;

$gross_price = 100; netprice=net_price = netprice=gross_price * (1 + SALES_TAX);

echo $net_price; // 108.5`Code language: PHP (php)

Try it

The following example uses the const keyword to define the RGB constant that holds an array:

`<?php

const RGB = ['red', 'green', 'blue'];`Code language: PHP (php)

define vs. const #

First, the define() is a function while the const is a language construct.

It means that the define() function defines a constant at run-time, whereas the const keyword defines a constant at compile time.

In other words, you can use the define() function to define a constant conditionally like this:

`<?php

if(condition) { define('WIDTH', '1140px'); }`Code language: PHP (php)

However, you cannot use the const keyword to define a constant this way. For example, the syntax of the following code is invalid:

`<?php

if(condition) { const WIDTH = '1140px'; }`Code language: PHP (php)

Second, the define() function allows you to define a constant with the name that comes from an expression. For example, the following defines three constants OPTION_1, OPTION_2, and OPTION_3 with the values 1, 2, and 3.

`<?php

define('PREFIX', 'OPTION');

define(PREFIX . '_1', 1); define(PREFIX . '_2', 2); define(PREFIX . '_3', 3);`Code language: PHP (php)

However, you cannot use the const keyword to define a constant name derived from an expression.

Unless you want to define a constant conditionally or use an expression, you can use the const keyword to define constants to clarify the code.

Note that you can use the const keyword to define constants inside classes.

Summary #

Did you find this tutorial useful?