Can you even store data?
The best way to introduce PHP variables is to start thinking about math. You might remember that there are these things called x and y variables that used to store numbers.
If x = 2 and y = 5 then what is x + y? Just substitute 2 for x and 5 for y and you get 7.
Variables are just storage containers. You can store any data type that you would like inside of them; we’ll look at strings and integers below.
In PHP, variables start with a dollar sign, followed by either the underscore or a letter, followed by a series of letters, numbers, and/or underscores. Let’s look at some examples of valid PHP variable names.
<?php // Valid PHP variable names $x = 1; $X = 2; # different from $x $_x = 2; $__y = 3; $_z_ = "Hello"; $__name__ = "Dino"; $dino1 = "First Dino"; $dino_1 = "Dino 1"; $_dino_1 = "He was number 1"; $d1_2_3_hello_ = "Still valid";// Invalid PHP variable names $1 = "Number 1"; $%abc = "Invalid"; // Really, any special character ?>
As a regular expression, the valid syntax for PHP variables is stated as:
You, of course, have to be familiar with regular expressions to understand the code above. If you don’t, just skip over it for now.
Create a new file and name it variables.php. Copy and paste the code below and open it up in your browser. What do you see? You should see 8.
<?php $x = 3; $y = 5;echo $x + $y; // same as saying echo 3 + 5; ?>
Let’s talk about the code above. Integer 3 is assigned to the $x variable. Integer 5 is assigned to the $y variable. Now that we have the two values assigned to the $x and $y variables, we can do something with those variables. In this example, we add the two variables together using the arithmetic operator, +, and then echo out the result on the screen.
If you’re completely new to programming, it sometimes seems like the person is skipping steps. For example, I know that we haven’t talked about the arithmetic operator or the concatenation operator that we’ll use next, but here we are using it in our examples. Some concepts are simple enough that you can understand what’s happening. Other times, you may have to read ahead and then return to the previous article to fully grasp what’s happening. Moving on.
We’ll look at one more example. We’ll store a string in the variable and then echo it out. First, we’ll assign the string to the $name variable and then concatenate it to the “Hi my name is” string. You can’t just place the variable next to the string and expect PHP to understand that you want to join the two strings. You use the concatenation operator (.) to do that.
<?php $name = "Dino Cajic";echo "Hi my name is" . $name; ?>
If you open up the code in your browser, you’ll see that the following text is displayed: Hi my name is Dino Cajic. You can change the value of the $name variable to whatever you want and it will automatically update the output.
Let’s do one more. Take a look at the code below. We’ll assign the year, make, and model of a vehicle to the $car_year, $car_brand, and $car_model variables. Since the R34 Nissan Skyline GTR is not legal in the United States yet, we’re going to want to check when it will be legal to import it. We’ll add 25 (how old the car has to be in order to be legal in the states) to the car’s year; we’ll also grab the current year with the help of the date() function and store it into the $current_year variable.
Below that, we’ll use a few echo statements to display the variable values on our browser.
You’ve had exposure to variables in math for most of your life, so you should feel right at home. Variables can get a little weird though. We’ll do a deeper dive into variables in the 12th and 13th articles.
Continue your learning with these articles
Should you document your code?
PHP has a few different options when it comes to comments. Frequently, it’s based on preference on which comment style you use, but other times it makes sense to use one over the other.
CAN YOU EVEN STORE DATA?
Variables are just storage containers. You can store any data type that you would like inside of them.
TRUE OR FALSE?
The boolean value is a scalar data type that represents the values true and false. That’s really it. End of article. That would be a short article so lets take a look at a few examples before we abruptly end it.