Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We are now going to take a look at how to display variables, not only how to display them, but how to create them initially in PHP. Every server side language, has a slightly different way to go about creating and outputting variables, so we'll take a look at the PHP version. Now once we defined and opened up a PHP page within Dreamweaver, we now have this PHP tab. If I opened a ColdFusion page, I would have a couple of tabs. If I opened an ASP page, I would have an ASP tab. So Dreamweaver recognizes the pages you're working in, and provides an extra tab in the Insert bar or two in the case of ColdFusion, to assist you in working with those server side languages.
So how do we create a variable, and what is it? Well let's take a look at adding PHP code in general. So, the reason I went to tab here, is because every piece of PHP must be surrounded by what's known as a PHP code block, and the easiest way to add that is in the PHP tab, it's this piece here. You can see it says code block when you hoover over it, so if you get confused about which one it is, just hoover over it, and it will say code block, and there is our code block.
I simply Clicked that and Dreamweaver added that code for me. Now personally I think it's very easy to get the order of these mixed up, or to not quite spell it correctly, and then your code would not work correctly. So, I tend to let Dreamweaver ever do that for me, I will leave that totally up to you. Now, how do we create a variable in PHP? What we do is put a dollar sign. And you can see, when I do that, these are PHP code hints. These are different types of variables available in PHP. And I'm actually not going to be using any of these, what I'm going to do is create a variable of my own.
And I'm going to type out Dollar f name, for first name, equals and in double quotes, put my name. So you go ahead and put your name, I don't think you need to take my name. And we're going to do the same thing for L name. L name representing last name. So go ahead and put your last name. Now, as I am typing, you may see some interesting things happening in the Dreamweaver interface. First of all, we had that little purple, kind of blinking so to speak. It kind of fades in and out, to test things.
If I go to type something, I'll do it again. So let's do dollar m name, equals. Now you can see as I type, there's this little red piece here, and there's information coming to me across this area. Right now I have not completed this line of code, so PHP is telling me, or actually Dreamweaver is understanding the PHP enough to tell me, that there's an error somewhere in this area. And you can see this says it's on line 13, and it's actually on line 12. So what's happening is I didn't complete this line, so PHP cannot read line 13. This is new.
This is new as of Dreamweaver CS5. Now, you don't have to have Dreamweaver CS5 or 5.5 in order to work through this course. I just want to point out some of the new features that have come out, and you'll see this one consistently as I type in code view. So just to fill you in on what that is all about, it's really handy to point out, and you can see now it says no syntax errors, to point out little errors that can occur as you're typing. So this is a great new Dreamweaver feature, as of CS5, it understands PHP and can try and assist you and point out errors.
So what have we done? We have created variables. Well, what does that mean? If you have programming experience, you understand, but I'm not assuming you do throughout this course. So what we have done is created a space in computer memory. And you can almost think of it like a big chest of drawers. So you have this dresser drawer and you open it up, there's first name is on the outside of the drawer and when you open it up, it can change.
The information inside that drawer can change. But the drawer itself is always going to be called F name. Now in our example, I would put my first name, for you, you'd put your first name. In other words the value of the variable changes which is this piece, but the name of the variable doesn't. So, basically a variable is a way to represent a value at a particular time. So we just created PHP variables. Let's take a look at our page. It doesn't matter which browser I preview in.
And we have absolutely nothing on the page, aren't you impressed with you, all of your hard work? Now what we need to do, is print it to the page. We need to tell PHP, take those values and put them on to the HTML page. And the way we print in PHP is to use the word echo. Echo tells PHP, print this to the page. And what I'm going to do, is put this into a sentence. And I'm going to say, my name is, $, f name. Now this is another new feature.
Any variables you create, you can see Dreamweaver understands it, it pops up as a code hint. And what I can do, is just Double-click it and it pops it in for me. So any variables you create, PHP will now understand. So, I'll put the L, there's my L name, and I can Double-click it, or you can type it out. Whatever is your preference. I'll put a period in that sentence. And the last piece of every PHP lines needs to be a semi colon. So you can see each one of these lines has a semi colon. So I'm telling PHP print out this sentence, but what PHP is going to do is replace the name of the variable with my first name, and the name of this variable with my last name.
So let's test it out and make sure that's the case. And there it is, your first sentence in PHP, if you haven't worked in PHP previously. Now there are different types of variables. We're going to be working mainly in these basic local variables, and to print to the page you use the word Echo. So there is how you create variables in PHP, and output them to the page.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98095 Viewers
61 Video lessons · 85367 Viewers
71 Video lessons · 69378 Viewers
56 Video lessons · 101735 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.