Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
If you remember back to the introduction where I talked about why you would want to use PHP. The real power of PHP comes from the ability to Output Dynamic Text. That's what makes it different than just an HTML website. If, for example, we have an auction website and a new item has been posted for sale, it gets stored in the database. PHP goes and grabs it out of the database, and now the HTML page that the user sees is different than what it was a few minutes ago. This new item has been dynamically added to the page. So we're going to start looking at how we can output dynamic text and we're going to start really simple by just outputting a few words.
And then we'll build up to being able to pull things from the database and to actually output more elaborate stuff. To start with, we're going to learn the most important PHP that you need to know, which is the echo command, or function. Echo is just simply going to turn whatever we say, back to the user, it's like an echo. We say something, that's what gets echoed back to the user so that they see it. You can think of it like printing it to the users browser and we'll use it inside our PHP tags with whatever we want to echo back. So for example open php tags echo space and then inside quotes, Hello world semicolon At the end and then close our PHP text. That will send to the users browser embed on the page Hello world no quotes just the characters from the H to the D Let's create a page where we can try this out. Now, instead of doing it on one of these other existing pages I'm going to go into my text editor, TextMate, and I'm going to choose New From Template. Now that's little different than what we did before, instead of just creating a new file I'm going to create an HTML file, and I'm going to pick the transitional version.
That's just going to give me some basic HTML at the top. I'm going to remove some of these bits. Make it a little simpler and do some indentation on it. And I'm going to save this file in my site's directory. I'm just going to save it as basic.html and that will be an HTML file that I can pull up any time I want to work with and to make a copy from. Now notice it's .html so we can't put PHP in it. But now I'm going to do Save As on that document. And I'll make a new one from it, and I'll call it helloworld.php.
Okay, so that's in the same folder, I now I have both of those. This one I'll be coming back to often when I really want to get some HTML again, and then this is the one we're working with now, .php. Ready for PHP to be embedded. Put Hello World at the top, and let's embed our php code here. Let's open up our tags, php echo, open the quotes, Hello World, exclamation point then the semicolon, and then close our php tags. So there we go, nice and simple. We'll save it, always important that you must save it, because Apache is going to read whatever is saved on the hard drive. Whatever's open in the text editor and not saved, that's only on our screen. Let's go into Firefox and let's open up localhost and kevinskoglund/helloworld.php, and there it is. Now lets do View Source on that.
Under Tools for web developer, I have the ability to do Page Source, and you can see that it output all of the HTML. And then right here, it just dropped in that text. No quotes around it, just dropped that text right into the body. So you see how we can embed PHP. That's what we would have expected to happen, it output it right there. Now if we take away the echo, if we just have the string by itself, and we reload the page, then it doesn't output it. We still get the HTML, we can still view our source. And see it's there, but it didn't output anything.
The echo is a very important part of outputting that to us. So let's try another one. We're going to learn how to concatenate two parts together. So we have Hello World, but this time let's put quotes here so that it's two different quoted strings. Hello inside quotes and World inside quotes, and I've got an extra space in here right before World. This period is going to concatenate them together, that is smash them together dynamically generating one new thing they get echoed back. This is a very common technique just to echo things and also to concatenate things together.
If we go here and we take a look, you see we get Hello World two times. Now, it's not on a new line. Right, there's nothing here to tell us to go to a new line, it just echoes one, then the white space. This is just HTML white space, it just says put one space in there. And then followed by our next command. If we want to actually have a line return, well then we need to format it with our HTML. We need to have like a br tag. So, it's a very important point just because we see a new line here between our PHP tags doesn't mean it generates a new line. We still have to write the HTML to make that happen. Alright, next I just want to show you that we can also generate some numbers. Let's try doing 2 plus 3 and we'll just return that back. Let's first take a look, let's put another br tag. Let's take a look at it this way, and notice that it didn't output anything. Well, it's because I didn't echo it.
I always need to echo it, it did the calculation, it did actually add 2 plus 3, but then it just threw away that value. It didn't do anything with it, because I didn't tell it to print it to the screen using echo. Now, the one last point that I want to make here is that the semicolon is a separator that I've told you, you always need. It's a good habit to always have in your PHP. In this case when it's a one line you don't actually need it. Apache is able to tell when it's parsing the PHP that this is the end of the PHP because we have an ending tag here. And it will keep it separate from this one because it's in a separate tag. So we don't strictly speaking have to have the semicolons. However, I think it's a really good practice to just always, always, always use the semicolons.
An that way you won't be tripped up by the sometimes that you use it and sometimes you don't. Just get in the habit of always using a semicolon, and you'll never run into problems. Now that we're able to output dynamic text, I want us to pause for a second, and take a look at how the web server processes these PHP pages. Because I think that process is going to be instructive. We'll do that in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99373 Viewers
56 Video lessons · 112631 Viewers
71 Video lessons · 81455 Viewers
131 Video lessons · 39104 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.