The concept of variables has been around since the dawn of mathematics and equations. In the most simple equation: x + 3 = 8, “x” is a variable, which can stand for any number in the world. Once computers were introduced, mathematics played an important role in the way they function and along with mathematics came the variable.
Variables in programming
The variable and its counterpart – the constant, now play a huge part in computer programming. Since computer programming plays a fundamental role on the Internet, it is the area where we could all sense the importance of both variables and constants.
Variables empower developers to quickly create a variety of simple and complex programs that tell the computer to behave in a pre-defined manner. Each program contains a list of variables and a list of directions where the variables are used to represent changeable data. The directions provide instructions how the variables should be used by the computer. The greatest advantage of the variables is that they enable one and the same program to execute various sets of data.
In the light of the afore-stated, a variable refers to a symbol for a varying value, which is stored in the system's memory. Each variable has a name (variable name) and a data type, which indicates what type of value it represents. The represented data can contain both numerical (including integers and floating-point numbers) and non-numerical values, such as letters and the '_' symbol.
The process of specifying and modifying variables in a computer program is called variable manipulation. It is mainly used for generating dynamic content through HTML (Hyper Text Mark-up Language) and other widely used programming languages. Some languages have strict data type requirements.
Variables in programming languages
-
PHP variables
In PHP, variables can contain numbers, strings or arrays. Once set, they can be used anywhere in your code. A great advantage of PHP in terms of variables is that they don't have to be defined specifically. The PHP interpreter will do that automatically, based on the variable's value.
PHP variables:
<?php
$myVar1 = "simple string";
//var $myVar1 will store a string
$myVar2 = 1432;
//var $myVar2 will store number
?>All variables in PHP must start with the "$" sigil. Otherwise, they will not be interpreted as variables. This makes it easier when reading a code to see which part of it is variable.
-
Perl variables
Perl works with the so called "scalar variables". These variables are really simple – they can contain only one element – a number, a string or an array. Perl also uses several sigils for different types of variables - "$" for simple scalar variables, "@" for arrays and "%" for hashes. The usage of different types of sigils for each variable type makes the Perl code easier to read and understand.
$my_string = "some string";
@array = ("element1","element2","element3");
%hash = (“forest”, “tree”, “well”, “bucket”);The hashes used in Perl resemble the arrays, with the difference that each element in a hash must have its own value. In our example, the value of the "forest" element is "tree".
-
Python variables
In Python, variables are declared without a sigil, but as a simple text.
some_text = “This is a string”
some_number = 358With the above lines we have just created two variables, the first one with the "This is a string" string as a value, and the second one with a numeric value - "358".
Python will also allow you to easily concatenate the values of variables.
word = 'Help' + 'A'
print wordThis will output:
HelpA C++ variables
Variables in C++ can only be created with data types such as integers or strings and contain data that can be presented with them. This restriction on the variable data types leads to faster development and more efficient program execution.
C++ variables:
//declaration of variables
int a1;
int a2 = 6;
float b1;
float b2 = 5.6;
char c1;
char c2 = 'u';-
Unix variables
There are two types of UNIX variables – shell variables and environment variables. The shell variables are local to each shell, and are not available to parent or child shells. The environment variables are preset, and are loaded with every process. They are available to child processes. Shell variables are usually defined in lowercase and environment variables are defined in upper case.
Bourne shell variables
In the Bourne family shells are treated in a different manner. When a shell is loaded, it will read all the environment variables and assign a shell variable for each one, using the same name and value. From there on, the shell will only refer to the shell variables and not the environment ones. However, if a change is made to one of the shell variables, it will automatically apply to the corresponding environment variable, since shell variables are only local for the shell. In order to apply the change, the variable will have to be exported.
UNIX shell variables
$ set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="1" [2]="17" [3]="1" [4]="release" [5]="i486-pc-linux-gnu")
BASH_VERSION='3.1.17(1)-release'
COLUMNS=176
CVSEDITOR=/usr/bin/vi
CVSROOT=/usr/local/cvsroot
DIRSTACK=()
EUID=0
GROUPS=()
HISTFILE=/root/.bash_history
HISTFILESIZE=500
HISTSIZE=500
HOME=/root
...
...
...
USER=root
$ echo $USER
root