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.
Author
Updated
5/20/2015Released
6/4/2013- 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
Duration
Views
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.
Related Courses
-
PHP with MySQL Beyond the Basics (2009)
with Kevin Skoglund10h 26m Intermediate -
Ajax with PHP: Add Dynamic Content to Websites
with Kevin Skoglund3h 37m Intermediate
-
Introduction
-
Welcome56s
-
Using the exercise files3m 8s
-
-
1. PHP Overview
-
What is PHP?3m 52s
-
The history of PHP2m 51s
-
Why choose PHP?4m 10s
-
Installation overview4m 13s
-
-
2. Mac Installation
-
Overview2m 33s
-
Changing the document root7m 24s
-
Installing to Yosemite8m 13s
-
Enabling PHP6m 16s
-
Upgrading PHP3m 30s
-
Configuring PHP10m 3s
-
Installing MySQL5m 46s
-
Configuring MySQL7m 24s
-
Text editor5m 1s
-
-
3. Windows Installation
-
Overview3m 27s
-
Installing WampServer5m 46s
-
Finding the document root2m 24s
-
Configuring PHP8m 12s
-
Configuring MySQL5m 45s
-
Text editor5m 51s
-
-
4. First Steps
-
Embedding PHP code on a page6m 43s
-
Outputting dynamic text5m 55s
-
The operational trail2m 27s
-
Inserting code comments4m 7s
-
-
5. Exploring Data Types
-
Variables7m 50s
-
Strings4m 38s
-
String functions8m 54s
-
Numbers part one: Integers6m 27s
-
Arrays10m
-
Associative arrays6m 37s
-
Array functions6m 33s
-
Booleans3m 50s
-
NULL and empty5m 15s
-
Type juggling and casting8m 27s
-
Constants4m 43s
-
-
6. Control Structures: Logical Expressions
-
Else and elseif statements4m 16s
-
Logical operators7m 30s
-
Switch statements9m 51s
-
7. Control Structures: Loops
-
While loops8m 41s
-
For loops5m 59s
-
Foreach loops8m 16s
-
Continue8m 28s
-
Break4m 8s
-
Understanding array pointers6m 43s
-
-
8. User-Defined Functions
-
Defining functions8m 25s
-
Function arguments5m 32s
-
Multiple return values4m 53s
-
-
9. Debugging
-
Common problems3m 47s
-
Warnings and errors8m 36s
-
-
10. Building Web Pages with PHP
-
Links and URLs5m 33s
-
Using GET values5m 35s
-
Encoding GET values8m 41s
-
Encoding for HTML9m 26s
-
Modifying headers6m 45s
-
Page redirection6m 43s
-
Output buffering7m 34s
-
-
11. Working with Forms and Form Data
-
Building forms7m 28s
-
Detecting form submissions5m 59s
-
Single-page form processing7m 57s
-
Validating form values10m 40s
-
Displaying validation errors7m 23s
-
Custom validation functions6m 28s
-
-
12. Working with Cookies and Sessions
-
Working with cookies2m 49s
-
Setting cookie values5m 55s
-
Reading cookie values6m 1s
-
Unsetting cookie values4m 51s
-
Working with sessions8m 29s
-
-
13. MySQL Basics
-
MySQL introduction6m 43s
-
Creating a database7m 41s
-
Creating a database table7m 42s
-
CRUD in MySQL5m 48s
-
Populating a MySQL database7m 32s
-
Relational database tables6m 40s
-
-
14. Using PHP to Access MySQL
-
Database APIs in PHP4m 51s
-
Connecting to MySQL with PHP7m 45s
-
Retrieving data from MySQL8m 47s
-
Working with retrieved data6m 12s
-
Creating records with PHP6m 58s
-
SQL injection3m 5s
-
Escaping strings for MySQL6m 45s
-
-
15. Building a Content Management System (CMS)
-
Blueprinting the application7m 19s
-
Building the CMS database5m 14s
-
Establishing your work area4m 38s
-
Making page assets reusable6m 36s
-
-
16. Using Site Navigation to Choose Content
-
17. Application CRUD
-
Refactoring the page selection10m 52s
-
Creating a new subject form6m 55s
-
Passing data in the session9m 16s
-
Validating form values9m 40s
-
Using single-page submission7m 44s
-
Deleting a subject9m 44s
-
Cleaning up10m 37s
-
Assignment: Pages CRUD4m 30s
-
18. Building the Public Area
-
The public appearance8m 52s
-
The public content area5m 51s
-
Protecting page visibility6m 57s
-
-
19. Regulating Page Access
-
Admin CRUD8m 41s
-
Encrypting passwords7m 26s
-
Salting passwords5m 42s
-
New PHP password functions3m 13s
-
Creating a login system11m 28s
-
Checking for authorization5m 48s
-
Creating a logout page5m 40s
-
Conclusion
-
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.
CancelTake 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.
Share this video
Embed this video
Video: Outputting dynamic text