Learn Web Development – The Super Simple Way

Learn web development by actually writing some code

In your index.php document, type the following and save:

<?php 
$a = 1;
$b = 2;
echo $a + $b;
?>

The <?php and ?> tags are special tags that tell Apache to parse out PHP code within. Basically, this is not HTML, it’s code to be processed by PHP between the opening PHP tag (<?php) and the closing PHP tag (?>). You’ll be writing these tags quite a bit.

The code written is basic algebra. You assign some values to variables and perform arithmetic functions. Variables can store more than integers. They can store decimal values, boolean values, and even strings. What are these data types? Well, they’re different types of data. That’s what we call them in the programming world. The most common ones are:

  1. integer (whole numbers + negative whole numbers)
  2. float (decimal numbers)
  3. string (text)
  4. boolean (a one bit integer value: 1 or 0, true or false)

You can create your own functions if you need to. Let’s write a function that will add two variables and return the added result.

...
function add_two_variables($a, $b) {
    $total = $a + $b;
    return $total;
}
?>

Notice the function add_two_variables takes in two parameters $a and $b, and does the same arithmetic as before, stores the value in the $total variable, but at the end, it returns their sum. Arithmetic in programming works a bit backwards. To the left of the equal sign is where we store the final result. To the right is where we do the work. We could have written it in one line of code as such:

...
function add_two_variables($a, $b) {
    return $a + $b;
}
?>

… but it’s easier to understand what’s happening in the first function.

To get the results to print out on the screen, you could have simply “echoed” the result to the screen instead of returning it, but for something like this, it’s much easier to utilize it as a function that simply returns a value.

After the function, still within the PHP tags, append the following and save:

...
echo add_two_variables(1, 2);
?>

And then refresh your browser. You will see “3” printed on the screen.

Concatenation

Adding numbers is fun, but let’s append some strings together with the numbers. To append strings in PHP, you don’t need to use the + (plus) symbol. You simply need to use the . (dot) operator as in the following example. Change the echo line you wrote above to this (notice strings are always in quotes):

...
echo "My result is: " . add_two_variables(1, 2);
?>

Now, when you refresh your browser, you should see:

My result is: 3

Play around with the concatenation and variable echoing. Here’s a neat example:

...
echo "My result of adding " . $a . " and " . $b . " is: " . add_two_variables($a, $b);
?>

Or simply:

...
echo $a . " + " . $b . " = " . add_two_variables($a, $b);
?>

There’s a neat trick to do when using double quotations. You can actually place simple variable names inside the quotation marks.

...
echo "$a + $b = " . add_two_variables($a, $b);
?>

Yes, this line is less writing, but it can get confusing when looking at it. It makes it a bit hard to distinguish variables from strings. Also, if you want to use the dollar sign in your string, you have to be careful.

This little shortcut doesn’t work with single quotes which is why a lot of developers prefer single quotes over doubles.

Next step, we’re going to learn about data.