The ability to output dynamic text is an important application of PHP. Dynamic text is text on a page that changes as alterations are made. An important device used for outputting dynamic text is the echo function, which returns inputted data back like an echo. Learn about outputting dynamic text and using the echo command in this PHP with MySQL online tutorial.
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.
- What is PHP?
- Installing and configuring PHP and MySQL
- Exploring data types
- Controlling code with logical expressions and loops
- Using PHP's built-in functions
- Writing custom functions
- Building dynamic webpages
- Working with forms and form data
- Using cookies and sessions to store data
- Connecting to MySQL with PHP
- Creating and editing database records
- Building a content management system
- Adding user authentication
Skill Level Beginner
Q: This course was revised on 6/4/2013. What changed?
A: The old version of this course was 6 years old and it was time for a complete revision, using PHP 5.4. (The tutorials will work with any version of PHP and covers any differences you might encounter). The author has also added updated installation instructions for Mac OS X Mountain Lion and Windows 8. The topics and end project are the same, but the code is slightly different. It also addresses frequently asked questions from the previous version.
Q: This course was updated on 5/20/2015. What changed?
A: We added one movie called "Changing the document root in Yosemite," which helps the Mac installation run more smoothly.
PHP with MySQL Beyond the Basics (2009)with Kevin Skoglund10h 26m Intermediate
Ajax with PHP: Add Dynamic Content to Websiteswith Kevin Skoglund3h 37m Intermediate
1. PHP Overview
2. Mac Installation
3. Windows Installation
4. First Steps
5. Exploring Data Types
6. Control Structures: Logical Expressions
7. Control Structures: Loops
8. User-Defined Functions
10. Building Web Pages with PHP
11. Working with Forms and Form Data
12. Working with Cookies and Sessions
13. MySQL Basics
14. Using PHP to Access MySQL
15. Building a Content Management System (CMS)
16. Using Site Navigation to Choose Content
17. Application CRUD
18. Building the Public Area
19. Regulating Page Access
Next steps2m 4s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.