How is a Variable Defined?
Let us start off by checking out what Wikipedia has to say about Variables:
In computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value; this separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during run time, and the value of the variable may thus change during the course of program execution.
I’m confused, what exactly is a variable to a human like me!?
Variables are a core component of all programming languages and serve the basic and important function of storing information. Let’s explore what they are as I attempt to explain all about them with some good depth. When trying to understand what a variable is, I find using a box or an envelope to be a good analogy. For instance, you might store money in a box. You throw $20 into a box and write “money_box” on it. Your variable is then “money_box” with a value of $20. Now what is “$20” when you start thinking with programming? It is a number, and more accurately, an Integer, a whole number. Knowing the type of data you’ll be storing in a variable is pretty important to keep in mind so that you use them correctly.
So you now understand that “money_box” represents $20, great. If we come up with a simple problem to solve with programming we can see some use for that. For now, pretend you are trying to save up for a new car. Your current money is known as “money_box” and your goal variable is “money_goal” with a value of “3000”. Now what happens when we get paid our check of $1337? Where do we stand compared to our goal? Let’s check with a simple php program.
// We first create our "piggy box" with $20 in it
$money_box = 20;
// We define our target amount as the price of a new car, $3000
$money_goal = 3000;
// The amount of money we get paid at work
$paycheck = 1337;
// How far away from our goal are we?
echo $money_goal - $money_box ; // 3000 - 20 (2980)
echo "<br>"; // Move to a new line
// Woohoo we just were paid $1337 via our job at Coder Corp, add the money to the box.
$money_box = $money_box + $paycheck; // 20 + 1337 (1357)
// How much do we have and how far away are we now?
echo $money_box; // 1357
echo $money_goal - $money_box; // 3000 - 1357 (1643)
// Well that is great, how many paychecks is that?
echo ceil(($money_goal - $money_box) / $paycheck); // 3000 - 1357 / 1337 (1.23, rounded up to 2)
As you can see, using variables we can perform basic operates such as addition, subtraction and division to calculate values. We also can update an existing variable with a new value so we (the programmers) don’t have to keep track of it. Now as I mentioned, you can do math using variables (or just values) when working in code. What might happen if you tried to add the text “bacon” to the value of “money_box”, like so: ?
$money_box = $money_box + "bacon";
Well, you will either get a crash-n-burning error in a strict language, or you will end up with “money_box” having a value of “20bacon”, or something to that effect. It is probably not what we desired to have happen. Variables have a type of data, and that makes sense. We can’t treat “20 + bacon” as math in the real world without treating “bacon” itself as a number. This is why knowing what data types are is important, so we can avoid trouble that can come around with different operations.
What kind of data types are there?
The most common data types you will encounter are Boolean, String, Integer, Float, Double, Array and Object.
A Boolean is the most simple. Its value is either True or False. Nothing else, ever.
A String represents text.
Integer is a whole number.
Float is a number that may reach 2 to 6 (usually) decimal places.
Double is a number that has much more decimal depth than floats do.
Arrays can be thought of as a box (variable) holding more boxes of any data type.
Objects are similar to arrays as they also hold other variables. However, objects have considerably more complexity here and going forward.
Why are data types important?
So you now have a general idea of what data types are in programming. Why they are so important to keep track of? When you are build a program you are going to have different functions working with data. Even a fairly simple program is going to expect input in a certain manner, and it will fail if it is given incorrect input. It will be helpful to keep this in mind when looking for errors in your code as your progress in your learning. After enough time and experience it is one of those things that will become (mostly) second nature.