Variables

In programming a variable is used to store a value. A variable is like a drawer in a filing cabinet. Suppose we put something in the top drawer of the filing cabinet. Let's call this variable topDrawer. Once we have a name for our drawer we don’t have to worry about what is in there constantly, we just open the drawer and have a look when we need to know. The contents (value) of the drawer can change. We can take out what is in there and replace it with something else. Again we don’t have to bother remembering what the new value is, we just open topDrawer and have a look. That is why computers are so great, they allow us to be lazy and forgetful. If someone asks me what I have in the top drawer I say, “Don’t ask me, just look in topDrawer”. Laziness (along with impatience and hubris) is allegedly one of the psychological cornerstones of computer programming.

 

When we put something in topDrawer we say we assign it a value. We do this with the assignment operator (=). Now imagine the unlikely office scenario where I have a giant styrofoam number seven. I need to file this away safely so I put it in my top drawer:
 

$topDrawer = 7;

 

Here topDrawer has been assigned a value of 7. You may have noticed that the variable name is preceded by a dollar sign ($). In the PHP language all variable names must start with a dollar sign. As soon as you see a dollar sign you know you are looking at a variable. Variable names can contain numbers, letters and underscores (as long as they start with a dollar sign followed immediately by a letter or underscore). Although the dollar sign makes variable names look a bit ugly in PHP compared to some other languages like Java, it does have advantages too as we will see.  We can tell what value a variable has by simply passing the variable name as an argument to echo:

<?php

$topDrawer = 7;

      echo $topDrawer;

?>

 

arrow

 

 


 

Something important to note here is that the assignment operator (=) is used to put a value into a variable. We use it when we want to shove something into a drawer. It does not mean equals (as it does in mathematics). This decision, taken in earlier days of computing, to use = for assignment has caused no end of confusion for novice programmers ever since. We will see the actual equals operator soon but for now just keep in mind that = means assignment and not equals. Confused? I don’t blame you.

Lets look at another simple operator ++, called the increment operator. This operator is nice because it doesn’t do much. It only does one simple thing. The increment operator takes a number and adds one to it. Simple. Here is an example:
 

$foo 
$foo++  ; //results in 3

Another way of writing the above, without using the increment operator, would be:

$foo 
$foo $foo 1;  //results in 3 

Now lets look at another PHP code example using the increment operator and (its lovely sister) the decrement (--) operator which takes away a value of one:

<?php

   $foo = 2 * 2;  

   $foo++;       

   $foo++;         

   $foo++;        

   $foo--;  

   echo $foo;

?>

 

arrow

 


 

This is a pretty nonsensical program but it illustrates something important about programming. In the program we do several things to the $foo variable. We multiply 2 and 2 and assign the result to $foo.  Then we increment $foo three times and decrement it once. Its hard to keep track of exactly what is in $foo at any given time. This is the dark side to putting something in a variable and forgetting about it. We may happily put a nice friendly 7 into topDrawer but come back later and pull out an unlucky thirteen which we didn’t expect. If we ended up with an unexpected result in the last example we could go back try and debug the problem. Suppose we had hoped to end up with a 7 instead of a 6. One simple thing would be to go back and add an call to echo with $foo as an argument after each line e.g.
 

<?php

$foo++;  

echo $foo;

$foo++;  

  echo $foo;

?>

Ok in this example this is pretty pointless, as its fairly easy to figure out what value should be in $foo just by looking at the code, but when a program gets complicated it often helps to stick in some calls to echo during development, so you can check what value is actually in a variable. It may not be the one you were expecting. If your program works but just doesn’t do what you thought it would, then you have made a conceptual error (or logic error). This is harder to track down than a bug (syntax error) which will cause your program to crash (or at least complain loudly).

Let’s add one more command to our toolkit for inspecting variables, the  var_dump function. In programming a function is something that takes some data, does some work with it, and produces an output based on that work. This is similar to the concept of a function in mathematics. Or we may be familiar with the concept of a function in everyday language as something that has a use or does a job. The var_dump does a useful job of giving us information about variables. The var in var_dump stands for variable and dump is a word in computing that means to show information that is being stored by the computer. var_dump takes a variable name as an argument and tells us the value of that variable and its type. Unlike the echo command we need to put the arguments to a function, in parenthesis:

<?php

//create variables with different types

     $foo = 77;

   $bar = 3.14;

   //show information about variables

var_dump($foo);

echo "<br>";

var_dump($bar);

?>

 

arrow

 

 

We can see that  var_dump is like echo in that it prints its result to the webpage. In the last example it tells us that the type of the $foo variable is int (integer) and its value is 77, while the type of the $bar variable is float (floating point) and its value is 3.14.

There are other handy PHP functions that you can use to debug code. Here are some you may want to learn more about:

An Introduction to Variables in PHP Part 1

An Introduction to Variables in PHP Part 2