PHP

You can declare constants in PHP using the define keyword.

define("name", value);

The following examples show how to declare constants in PHP.

Notice: Please note that PHP requires that all statements be terminated with a semicolon ( ; ).

Once a constant is defined, its value cannot be changed while the program is running.

Java

You can declare constants in Java using the sequence of keywords static and final.

static final type name = value;

The following examples show how to declare constants in Java.

Notice: Please note that Java requires that all statements be terminated with a semicolon ( ; ).

Notice: Please note that in Java  you assign a value to a constant of type String using double quotes (”  “), but you assign a value to a constant of type char using single quotes (‘  ‘).

Once a constant is defined, its value cannot be changed while the program is running.

C++

You can declare constants in C++ using the keyword const.

const type name = value;

The following examples show how to declare constants in C++.

Notice: Please note that C++ requires that all statements be terminated with a semicolon ( ; ).

Once a constant is defined, its value cannot be changed while the program is running.

C#

You can declare constants in C# using the keyword const.

const type name = value;

The following examples show how to declare constants in C#.

Notice: Please note that C++ requires that all statements be terminated with a semicolon ( ; ).

Once a constant is defined, its value cannot be changed while the program is running.

Visual Basic

You can declare constants in Visual Basic using the keyword Const.

Const name = value

The following examples show how to declare constants in Visual Basic.

Once a constant is defined, its value cannot be changed while the program is running.

Python

You must accept it! You cannot declare constants in Python, there is no such option! However, you can still use variables (in uppercase) instead and take care not to alter their initial values each time you use these variables in your program.

The following example shows how to declare variables (used as constants) in Python.

Remember! In computer programming, once a constant is defined, its value cannot be changed while the program is running. Unfortunately, this is not true for Python. Since you use a variable in place of a constant, you must be careful not to accidentally alter their initial values below in your programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!