Difference between Variable and Constant


Difference between Variable and Constant

 

Difference between Variable and Constant

 

What is Variable

 

A variable is used to store different types of information and data in any programming language. We used the assignment operator (=) to assign a variable value.

 

Rules for declaring variables

 

  • A variable name must begin with a letter or an underscore (_).
  • A variable can have an alpha-numeric and an underscore (_).
  • The name of the variable should not start with a digit.
  • There should be no space between the name of the variable.
  • Variable names are case sensitive Example myvar or MYVAR are different variable names.
  • The variable name cannot contain any keyword that is used in a programming language.

 

How to declare variables

 

PHP is a loosely typed language; we do not need to declare its data type. PHP variable must start with the dollar ($) symbol.

 

Example - 

 

$php_variable = “value”;

$number = 5;

 

Declare variables in C++ programming.

 

type name = value;

 

Example - 

 

int num = 5;  string name = “E-siksha”;

 

Is equivalent to

 

int num; string name;

num = 5; name = “E-siksha”;

 

Declare variables in C# programming

 

type name = value;

 

Example - 

 

int num = 5;  string language = “C Sharp”;

 

Is equivalent to

 

int num;  string language;

num = 5 name = “C Sharp”;

 

What is Constant

 

Constants are similar to Variables but they have a fixed value that can’t change during the execution of the program script.

 

Rules for declaring constants

 

  • A constant name contains letters, numbers, or an underscore (_).
  • Constant names are case sensitive Example myconst or MYCONST are different constant names.
  • There should be no space between the name of the constant.
  • A constant name must begin with a letter or an underscore (_).
  • Numbers are allowed in constant name but not in beginning.

 

How to declare constants

 

Using the define keyword we can declare constants in PHP.

 

define (name, value, case-insensitive);

 

Parameters:

  •  name:  It Specifies the name of the constant.
  •  value: It Specifies the value of constant.
  • case-insensitive: Its default value is false

 

Example:

 

define (“MSG”, “Welcome to e-siksha.in”);

define (“pie”, “3.14”);

 

Read Also: PHP Constants

 

In C++ programming by using the const keyword we can declare constant.

 

const type name = value;

 

Example - 

 

const double pie = 3.14;

const int NUMBER_OF_PLAYERS = 11;

const string MSG = “Welcome to e-siksha.in”;

 

In C# programming we also used the const keyword to declare constant.

 

const type name = value;

 

Example - 

 

const double NUMBER = 30.05;

const int AGE = 12;

 

Read Also: C# Variables and Constants

 

Main Difference between Variable and Constant

 

Variables Constants
A variable is used to store different types of information and data. Constants are similar to Variables but they have a fixed value.
Variable represents the unknown value. Constant represents the known value.
Variable can be changed by the program once it is defined. Constant cannot be changed by the program once it is defined.