There are plenty of terms used in programming. These are pretty fundamental:
- variables are containers for data which will be manipulated and changed by scripts
- constants are the same thing but they hold values which will not change
- literals are raw data
An example would be the string literal "Fred" which can be used directly or stored in a constant called "username". A variable called "timeofday" could be used to hold data such as "morning" or "afternoon".
You could then tell the script to combine the constant, variable and a string literal to show a message:
Good morning Fred.
The word Good was in the form of a string literal (literally a string of text). The word morning came from the variable. The name came from the constant. In PHP these three might be used to display the message in this way:
echo "Good $timeofday USERNAME";
In PHP variables are shown by the dollar sign and constants are traditionally typed in upper case. Most people just use variables and don't bother with constants in PHP.
Declaring and assigning
Variables are created and then filled with data. In some languages you have to do both stages and in some you can skip the declaring as the interpreter will do it for you anyway. A declared variable will be empty or undefined. It can then be assigned data to hold. In JavaScript the two steps can be done on one line:
var price='£2.50';
That creates a variable called price and then assigns some data to it. The value being assigned is normally held in quotes unless it is a number.
Variable types
Some programming and scripting languages have very strict types of variables. When you declare a variable you need to say what type it is and then it will only hold that type of data. For example, declare a numeric variable and it will not be able to hold text. PHP and JavaScript tend to be much more relaxed than this. You can set the type of variables but on the whole most people just create a variable and the interpreter will guess what type it is based on what it contains. It will even change the type depending what you try to do with it (3 could be a number or it could be part of a string of text).
The most common types of variable are:
- string - these hold text (which may include numbers etc.)
- integer - for holding whole numbers (e.g. 4 or 13492)
- floating point - for holding numbers with a decimal point (e.g. 34.234)
- boolean - hold only TRUE or FALSE (or YES/NO or 1/0)
Global and local variables
Depending on the language the scope of a variable may be set in different ways. The basic principle remains the same though:
- global variables are available anywhere in the page/script/program
- local variables are only available within the function where they are declared
Global variables allow quick and easy access to variables from anywhere. However, they also make it very easy for mistakes to happen. With large scripts (perhaps including external libraries) it becomes likely that variable names will clash and data will be lost.
The best approach is normally to use local variables and then pass the values to functions as needed ( you will learn how to do this).




