Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
I want to talk about the concept of a variable in a programming language. What it means and also what the limitations are for working with PHP variables in terms of what you can name them and how you reference them. PHP uses variables just like any other language does. And what is a variable? Well, we're going to go out and we're going to create a page that uses variables. So, let's create a new PHP page, we'll save this as Create Variables.
We need to give it a PHP extension. And here is our HTML page, where we're going to place our PHP code. Now, what is a variable? A variable is a place holder for information within computer memory. I like to equate it to a chest of drawers or a dresser. So, you have a dresser where you store your clothing. Imagine the front of the dresser having a name, so each drawer has its own name. When you open that dresser, you can put different things inside the dresser drawer.
But the name of the drawer is not going to change just what's inside of the drawer, that's what a variable is. It's a way to store data to be able to reference it under a single name, but that data can change overtime. Let's create a PHP variable so you can see what I'm talking about. Now PHP, we have a special tab up in the Insert bar, and this Insert bar, you can open and close through window Insert as long as it's a PHP page that's open, your PHP tab will show. And I like to use this to create the code block.
I mentioned when we were talking about the difference between PHP and HTML, that PHP resides within an HTML page. So, here is our HTML page. So, we're going to add a PHP code block between the two body tags. So, I'm going to go up to the PHP tab. I'm going to click on this icon, right in front of Echo that says, Code Block. And when I do, I get a bright red code block for PHP. And I'll go ahead and make some space between those two. This is known as the opening part of the code block. This is the closing portion and all PHP code goes in between there. So, when the PHP reads through the page for processing, what it's doing is looking for these opening and closing tags and processing whatever is inside of them.
So, how do we create a variable in PHP? What I'm going to do is put a dollar sign, and you can see as soon as I do that, these are different types of PHP variables. We're just going to create a basic, local variable. And I'm going to name it Hello. And give it the words hello. Now if you notice, as I'm typing, this turns orange and yellow. This also gets a red mark. That is PHP not working correctly.
In other words, Dreamweaver is reading my PHP and letting me know that this is not a perfect line of code. And I do know that because I'm not finished. But this is a very helpful feature. Notice as soon as I complete the line, it now says no syntax errors. But this helps you recognize if there is an error and where it's located. What line has the error. I find it to be very helpful especially if you're new to a language.
So, there is a variable called hello, so you can think of this as the name on the desk drawer. All PHP variables begin with the dollar sign, and you do have to be careful what characters you put into your variable names. I will be using all text in all of our variable names. So, there are no errors. Now, I'm going to type world, and in double quotes on the other side of the equal sign I'm going to put world.
Now, what I meant by that desk drawer or that dresser drawer, storing different information, it's that this point in time, this is what this variable means. But I could also change it to my name. I could change this one to a different value. It doesn't matter what the value is. We are always referencing that particular item by its name and that's how PHP is going to understand what we're talking about.
So, let's take a look at this page so far, we'll put it in the browser, and see what we have, which is absolutely nothing. I am so proud. (LAUGH) We will have to look at how we can display PHP coding. It looks like we did absolutely nothing because nothing is being displayed, but it's not being displayed because we did not tell PHP to display it. So, we created two variables, one called Hello and one called World. We changed the values of the variables.
Those values over here are going in double quotes, and lines within PHP require semicolon at the end.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98139 Viewers
61 Video lessons · 85445 Viewers
71 Video lessons · 69415 Viewers
56 Video lessons · 101769 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.