Differences between variables and constants in PHP
  John Mwaniki /   08 Dec 2021

Differences between variables and constants in PHP

PHP Variables

A variable is an identifier (a symbolic name) of a container that holds data in a computer program and whose value can change during the program's execution.

The term 'container' refers to a computer memory location.

A variable can store different types of data such as numeric(integer, float, double), text(char, string), boolean, array, object, etc.

Unlike in most languages, in PHP, variables are not declared. A PHP variable is created at the moment of assigning a value to it.

You can give your PHP variables any name you want but it must follow these conditions:

  • It must start with a dollar sign $.
  • The variable name can only start with (after $) a letter/alphabet or an underscore _. It can never start with a number.
  • The variable name can only consist of alphanumeric (alphabets and numbers) characters and underscores.

A variable in PHP is created by writing its name and assigning it a value. A value is assigned to a PHP variable through the use of the assignment operator(equal sign =).

Examples

<?php
$name = "John Doe";
$_age = 30;
$total2021 = 143;

PHP Constants

Constants are like variables (being data containers) except that once they are defined (assigned a value), that value cannot be changed during the execution of the program.

A constant's name doesn't have a dollar $ sign. It can only start with a letter or an underscore. Similar to variable names, a constant name can only consist of alphanumeric characters and underscores.

By convention, constant's names in PHP are usually written in uppercase but can be of any case. They are case-sensitive by default but can be set otherwise.

Constants in PHP are created and defined through the use of the define() function.

Syntax

define(name, value, case-insensitive)

The description of parameters.

Parameter Description
name Specifies the name of the constant
value Specifies the value of the constant
case-insensitive This is an optional parameter. It specifies whether the constant's name should be case-insensitive. It's set to false by default meaning the constant name should only be used in exactly the case it is defined. If set to true, the constant's name becomes case-insensitive.

Example 1: Case-sensitive

<?php
define("NAME", "John Doe");
echo NAME; //Output: John Doe

echo name; //Output: name

We have defined the above constant as case-sensitive. Therefore, name is not a recognized constant. The output for echo name; is "name", which also logs the error below.

PHP Warning: Use of undefined constant name - assumed 'name' (this will throw an Error in a future version of PHP).

Example 2: Case-insensitive

<?php
define("NAME", "John Doe", true);
echo NAME; //Output: John Doe

echo name; //Output: John Doe

echo Name; //Output: John Doe

In the above example, we have defined the constant NAME and set its name to case-insensitive. Therefore, the above 3 outputs are the same regardless of the case used for the constant.

Unlike variables, constants are automatically global and can be used across the entire script.

In the early days, PHP constants were only used to handle simple text values. However, you can now give a constant an array value.

Example

<?php
define("students", array("Peter","Mary","Khan","Aladeen"));
echo students[3];
//Output: Aladeen
echo "<br>";

define("laptops", ["Apple", "HP", "Lenovo", "Dell"]);
echo laptops[1];
//Output: HP
echo "<br>";

define("person", array("name" => "Philip","age" => 40, "sex" => "Male"));
echo person["name"];
//Output: Philip
echo "<br>";

Now that you have an understanding of what variables and constants are in PHP and how they work, let's now cover their differences.

The differences between Variables and Constants in PHP

Variable Constant
The value of a variable can change during the execution of the program. The value of a constant cannot be changed during the program execution.
Variable names always start with a dollar sign $ Constant names do not start with a dollar sign (can only start with a letter or underscore).
Variables are defined through the use of the assignment operator =. Constants are defined using the define() function.
PHP variables are not automatically global in the entire script. PHP constants are automatically global across the entire script.