This is part 2 in a series of articles aimed at helping you get started programming with PHP. In part 1 of the series, I covered the basic building blocks of a PHP script, and then showed how to write a simple Hello World! program in PHP. In this article, I will cover one of the most basic tools for creating more interesting programs: variables.
Variables are used to store data inside of your program. This data can be a wide variety of things, such as numbers, character strings, or even lists. In PHP, variables must begin with a dollar sign, followed by the name of the variable. The variable name must begin with either a letter or an underscore, followed by any combination of letters, numbers and underscores. Here are a few examples of valid variable names:
To make your programs more readable, it is usually a good idea to give your variables names that indicate what they are being used for. If you had a variable that was used to keep track of the subtotal for a sale, you might name it something like $subtotal. One important thing to note is that the variable name is case-sensitive, so $myvariable and $myVArIabLe would be seen as 2 different variables. It is usually a good idea to pick a naming convention, and stick with it. That way you don’t end up digging through your code to find a bug, trying to figure out which variables are being used for what.
Declaring and Initializing Variables
When we declare a variable in PHP, we can also assign a value to that variable at the same time. Let’s take a look at an example:
$variable1 = 7;
$variable2 = "abra cadabra";
After this code is executed, $variable1 contains the number 7, and $variable2 contains the character string “abra cadabra”.
Basic Variable Operations
Once we have initialized our variables, there are a number of operations that we can perform on them. For numeric variables, we can perform mathematical operations:
$variable1 = 4;
$variable2 = 2;
echo $variable1 + $variable2;
echo $variable1 - $variable2;
echo $variable1 * $variable2;
echo $variable1 / $variable2;
As we learned in the previous article, the echo function displays in the web browser whatever comes after it. The 4 echo commands in the code above would display 6, 2, 8 and 2, respectively.
We can perform similar operations on character strings, although it doesn’t make much sense to multiply 2 character strings together (what would “Hello” * “World!” equal?). Instead, we can concatenate 2 strings together using a period;
$variable1 = "Hello";
$variable2 = " World!";
echo $variable1 . $variable2;
The echo command above would result in “Hello World!” being displayed to the screen. Not very exciting, I know, but I’m sure you can think of some more interesting uses for this.
For Next Time
After reading the first 2 articles in this series, you should be able to start writing programs in PHP that are a little more interesting. Try thinking of some more interesting uses for variables than just what I showed in the examples. Stay tuned for the next article in this series, which will focus on program control structures (if/else blocks, for/while loops). Now we’re really getting somewhere!
You can already read Getting Started with PHP: Part 3 – Program Control Structures.