Webshop Blog
PHP From the Top: 2 (Using Variables and Simple Math)

This is the second installment of this new series I’m writing called “PHP From the Top.” Hopefully this guide will become a resource for beginners to get a handle on the PHP language and how to use it. I’m going to do my absolute best to not gloss over little details as I’ve found that many times beginners simply over look something minute yet crucial, get frustrated and the give up. I want to do my best to avoid this, and if any readers come across anything of the like, please feel free to discuss it in the comments. Anyway, today we’re going to cover the basics of the PHP language and get our hands dirty with a little bit of code for the first time.
If you’re new to the PHP language you may want to read the first article in this series on how to get set up with a PHP development environment on your machine. But, if you’re already set up and ready to go, we’re going to dive in.
NOTE: I will assume you know HTML and CSS in that I wont go over those elements used as supplements to these articles, however if you do not know HTML and CSS it should not be detrimental to your understanding of the concepts outlined here.
Other Posts in this Series
- 1: Getting Set Up
- 2: Using Variables and Simple Math
- 3: Loops and Conditions
Getting Started
First of all, PHP’s opening and closing tags look like this: <?php and ?>. This is very similar to a how an HTML tag works, you write your code between the two tags like the example below:
<?php //This is where you write your PHP code. ?>
Anytime you write PHP code, it must be between these two tags. For the rest of this article and this series, I will omit the opening and closing <?php and ?> tags from the code examples. Just remember that they must be there for your code to properly execute.
Printing a Message
In PHP there are two ways to print data to the screen. echo and print. Technically, you can use whichever one you wish, there are arguments for both sides which I won’t get into now (maybe in another article), but I will use echo throughout this article and throughout this series.
Printing output to the screen in PHP is extremely simple and easy all you have to do is write:
echo "Print this message to the screen.";
Now there are a few things here I want you to notice and understand with this line of code. First, the message is enclosed in quotation marks. You may use double or single quotation marks, there are pros and cons to using either one of them, just make sure you stay consistent. Second, the line ends in a semicolon ;. Every line of PHP code you write must end in a semicolon. This tells the parser that this is the end of a line (the end of a command) and now it can move on to the next one. This is because PHP is whitespace insensitive, and doesnt care if you add spaces, line breaks, or tabs within your code. Ultimately, any whitespace, unless encapsulated with quotation marks, will be ignored.
Commenting Your Code
While programming you may want to leave a note in your code, perhaps to yourself or to someone else that may look at your code in the future. This can be done by using comments. Comments are ignored by the parser and therefore only show up in the code. You can comment out a line, a few lines, or whole blocks of code. In PHP, comments are declared using two methods, if you only want to comment out one line, precede the text you want commented with two forward slashes //, if you want to comment out more than one line you add a forward slash followed by an asterisk /* to the beginning of the comment, and an asterisk followed by a forward slash */at the end of the comment.
//This is a single line comment. This whole line will be ignored. /*This is a multiple line comment All of these lines will be ignored*/
It is generally considered good practice to leave comments in your code, this enhances code readability. It will help others navigate and understand your code more quickly and it will help you not forget why you did something a certain way. I will use comments in the example code throughout this series for code explanation.
Executing Your Code
PHP code can be written right next to your HTML code in the same file, and the other way around, you just have to remember that if you’re using PHP code in a file you have to change the filename to .php instead of .html. (If you write PHP code in a .html file, it will simply be ignored.) This is fundamentally important. You have to remember that PHP is a Server-side scripting language, this means that the code is actually executed before it reaches your user’s web browser. This is important to understand because when your PHP code outputs a message, that message is printed to the HTML file that your users will view in their browsers. You can actually have PHP output HTML tags to the browser. You can do it like this:
echo "<strong>This text will be bold</strong>";
Simple Math in PHP
Performing mathematical calculations in PHP is very easy, and the operators may already be familiar to you if you have ever had to do math on a computer. Let’s take a look at an example.
echo 1 + 1; //prints the number "2"
Let’s look at some of the operators we can use.
echo 3 + 2; //Addition, prints the number "5" echo 3 - 2; //Subtraction, prints the number "1" echo 3 * 2; //Multiplication, prints the number "6" echo 3 / 2; //Division, prints the number "1.5" echo 3 % 2; //Modulus or Remainder Operator, prints the number "1"
Note that PHP does follow the traditional mathematical order of operations. If you want to alter the order in with your calculation is performed, use parentheses.
echo 3 + 2 * 4; //prints 11 echo (3 + 2) * 4; //prints 20
Using Variables
In PHP, variables are defined by preceding the name of the variable with a dollar sign: $. Variable names may only contain alphanumeric characters or underscores, they must start with a lowercase alphabetic character, and may not contain spaces. PHP is a dynamically typed language, which means that you do not need to declare variable types, you simple declare the variable name and its value. A variable can be assigned many different values types: integer (a whole number), float (a decimal number), string (text) and a few others that we’ll get into later on in this series. Let’s look at a few examples of assigning different data types to variables.
$int = 4; //this variable holds 4 $float = 1.5; //this variable holds 1.5 $string = "This is a string."; //this variable holds "This is a string."
You can perform math with variables, just as you would expect (as long as the variables contain number values).
$int1 = 5; $int2 = 7; echo $int1 + $int2; //outputs 12 $int3 = $int1 + $int2; echo $int3; //also outputs 12
Putting it All Together
I know this article got a bit lengthy, its a lot of information to take in at one sitting. Read through it again, and really take your time to make sure you understand it. This is the foundation of understanding the PHP language. Now that you know how to declare a php code block, how to output data to the screen, how to comment your code, how to perform simple math and use variables you are able to build simple PHP scripts from scratch. Take some time to play with these concepts in your own applications. If you have any questions about anything discussed in this article or anything else regarding PHP, feel free to ask them in the comments.

know this article got a bit lengthy, its a lot of information to take in at one sitting. Read through it again, and really take your time to make sure you understand it. This is the foundation of understanding the PHP language. Now that you know how to declare a php code block, how to output data to the screen, how to comment your code, how to perform simple math and use variables you are able to build simple PHP scripts from scratch. Take some time to play with these concepts in your own applications. If you have any questions about anything discussed in this article or anything else regarding PHP, feel free to ask them in the comments.