Constants in PHP | Types of Constants | Define Constants

Variables & Data-types in PHP

What is a constant?

A constant is an entity that does not change its value. Hence, In PHP we create constants so that we cannot change its value after that. In this way, unlike variables, the values will not be overlapped with the same constant name.

What is a PHP constant?

For example, like variables if the code is: –

$a= “book”; 
$a= “axe”; 
Echo $a; (here the value will be print ‘ax’ because this is the last value given to variable) 
$a= “jump”;
Echo $a; (now value ‘jump’ will be print ) (the reason is they are variables. They will change their values whenever a new value is assigned)

But what if you don’t want to change values at any time. In that case, we will need a constant variable(the variables which are variables but are constant).

Syntax to write constant variable
define(name, value, case-sensitive)
*name- constant variable name
*value- constant variable value
*case sensitive- true/false

A constant variable has three parameters. Name, value, and case-sensitive.

Case sensitive:- As we know cases affect the output of the program. So, we should always try to write our code in the same cases. But in the constant variable, we can write in a different case and expect the correct output. This is known as the case sensitivity parameter.

True means case differentiation will not affect the output of the program. For Example, $age will be the same as $AGE.

False means case differentiation will affect the output of the program. For Example, $age will be different from $AGE.

Rules to define constant variable names: –
1. It cannot start with the ‘$’ sign.
2. It can start with only ‘letter’ or ‘underscore(_)’
3. Constant variables are Global Variables

*(Global Variables will be discussed later)

Example to write code with constant variable.

The code will print the value ‘100’.

Now define another variable with the same name.

The code will print this error Notice: Constant value already defined in C:\xampp\htdocs\constant.PHP on line 3 100

Instead, you can write your code like this:-

It will print values ‘100’ and ‘150’

Don’t write your code like this as well. It will only result in an error:-

You can also do calculations:-

It will print value ‘120’. These are known as arithmetic operations. It will be discussed next.

How constant variables are case sensitive

Example program

The print will result in this error Warning: Use of undefined constant Value – assumed ‘Value’ (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\constant.php on line 4

Value

Here value is not as same as Value. So, the server will consider them different variables.

But if we add another parameter as true, the same above program will run and will not result in error.

Now, the same program will print value ‘100’

Difference between constants and variables

  • A constant can be defined once and does not change its value but a variable can change its value multiple times.
  • There is no need to use dollar sign($) before constants while variables always use a dollar sign.
  • Constants are defined by using define() function.
  • Constants don’t need to follow any variable scoping rules and can be defined anywhere.
Constants in PHP | Types of Constants | Define Constants
Show Buttons
Hide Buttons