For this lesson, you will need:

  1. Access to an online PHP interpreter. I’ll be using OneCompiler for this example. View my quick overview of OneCompiler
  2. A basic primer on math in PHP – These articles should do the trick
    1. Tutorials point – Working with PHP operators. Just the section on Arithmetic Operators
    2. Tutorials Point – PHP Math Operations Just the section on Performing Math Operations
  3. A cup of coffee ☕️

What are variables?


What are variables good for?


How should I name variables?

In short, you should name your variables with the shortest name you can come up with that describes exactly what the variable is. 

Say I am working on a small app to calculate sales tax. I know my price may change, I may have a discount which can also change, and I have a sales tax, which may vary depending on where my user lives.
It can be tempting to keep those variable names as short as possible.


$p = 5.00;
$st = .032;
$d = .05;

You will see this often in examples online.  In fact, you’ll see it so often that it might seem like the convention. This is not the case! Instead, make those names specific and easy to recognize and reproduce in other places (i.e. check your spelling)

$price = 5.00;
$sales_tax = .032;
$discount = .05;

In my original version,  when I want to calculate the total cost for this, I end up with something like this:

$p = 5.00; // Price
$st = .032; // Sales Tax
$d = .5; // Discounts
$pmd = $p - ($p * $d); // price minus discount
$total = $pmd + ($pmd * $st); // price + Sales tax
echo $total;

You could argue that this is fine, and it sort of is. It will run just fine. If your script is only ever going to be 6 lines of code, this is totally passable.

Here’s the problem:

Your script will almost never be just these 6 lines of code. It may start that way, but as you want to do new, interesting things with your code; It. Will. Grow.

You will want to do new, interesting things. Always.

Your code will grow, and so will you. You will grow to hate more and more every time you have to scroll through 100 or 500, or even a thousand lines of code to remember what each of those values (and several dozen more) represents.

[add vs code multi-select example]