Main Content

PHP variables - dynamically typed. What does that mean?

Archive - Originally posted on "The Horse's Mouth" - 2012-11-08 12:50:32 - Graham Ellis

When we're programming, we store data in our computer memory in one step of our process, then read it back for further reference or processing in a later step. In the very early days, this was planned out using the numeric location in the computer memory, but very rapidly programming languages were developed where we name rather than number locations. This change allowed for relocatable code to be written which could run at different places at diffent times - vital on timeshared computers that have many different programs running at the same time. Naming also makes life very much easier for the programmer in that his program will refer to stored data by a name of his choosing - typically a descriptive word or some sort - rather than by an obscure number.

When writing in many languages, the programmer specifies the name of each variable he'll be using, and the type of data it's going to contain. Computers work with patterns or 0s and 1s (binary), and a group of 32 of these could contain a whole number (also known as an integer), a real number (a float), a string of 4 characters (ASCII text) or various other things. That's why the type is declared - so that the correct interprettation is put onto the bit pattern later on n the program. Languages like C and C++ work in this way - the code runs very fast, but it can be quite slow to actually write it. [C Courses].

As well as it being a lot of extra work to declare all your variables, and their types, ahead of time, you need to know how much memory you'll need as you write your program. If I'm going to allocate up to 10 float values for the ages of pets (if we're writing some sort of vetinary application), then that puts a limit on the program and we have a problem when the woman with 27 cats.

The solution applied in PHP (and Perl, Python, Ruby and Lua of the languages we teach) is to use dynamic variable allocation and typing


When a variable is first referenced within a program, memory is allocated for its use. The type of data to be stored in the variable can be determined by the context of the program code in which it appears, and as well as the data being stored, there are some extra bits - always in the same place - which indicate the type of data. This makes for code that runs a little bit slower, but is far quicker to run. And it allows for the language to be easily and natureally extended to allow for changing the type (and size) of what the variable contains even as the program runs.

In PHP, variables can contain whole numbers (integers), numbers with decimals (floats), strings of text, and arrays ... and a whole load of other things too like objects, bool (boolean) and resources - such as file handles.


Explanation from one of our PHP courses running this week. Opening image - our cat Charlie, who really does not want to be one of 27!