PHP code is often required for some HTML pages to function correctly. Fortunately, PHP can easily be embedded into an HTML page with a PHP tag. These PHP tags turn on PHP and process the code, then turn off PHP once the code has been processed. Learn about the fundamental rules of embedding PHP code on a page in this PHP with MySQL online tutorial.
Now that we have PHP installed. Were ready to start learning how to use it. We're going to start by learning how to embed PHP code in a page. Because there's some basic rules that we need to know about. We already did this in the setup chapter. We created our first file which was my_PHPinfo.PHP. And we populate with just this one little snippet of code. And you may not have completely understood what it was at the time, you may have just followed along. That's okay were now going to talk about it, and talk about how this works. Notice that there's the beginning, and ending to this line with a bit of code in the middle.
I'll drop out the code in the middle, so you can really see the difference. This is the beginning of opening a PHP tag, and the end, closing PHP. What we're doing is essentially saying to the Apache server, hey, as you're processing this document, turn on PHP. Start reading the next little bit as being PHP code, and then when it gets to the end and it sees that question mark with the greater than, The tag is over. Now we're done with PHP. You can go back to doing your regular html rendering.
This allows us to embed PHP into the html. Remember, in the early introduction I talked about taht that's one of the features of PHP. Is that we can just embed in the html code. So we're essentially telling Apache to turn on and turn off PHP filtering as it's going through the document. And then in between, we can put whatever PHP code that we want. So if you look at somebody else's PHP code,you're going to see these open and closing tags throughout the document, as it's turned on and off. This isn't the only way that you can write these PHP tags. There's also something called Short-Open Tags.
These, I would say, are considered bad form. This is using the same tag, but omitting the letters PHP from it. There's also a form that uses the question mark followed by an equal sign which outputs the results directly. normally evaluating the PHP doesn't output results on its own, you have to tell it specifically I want to output something here, otherwise you could just do some processing, you could add one plus one... and not out put the results and do a calculation in there for the second for Mr. this has become more popular especially to a lot of experienced PHP developers has started to prefer having the shortcut but it's still a bad idea and we will talk about why just a second The other way that you could do it is to use ASP style tags. I think this is considered very poor form, instead of using the question mark, we use the percent sign.
ASP is Microsoft's version of PHP. It's what became asp.net. ASP was something that actually existed before asp.net. And this is the way that the tags look. They have the percents on either side. And again we have that equals there. Both of these options are something you can turn on and configure in your PHP INI file to allow them. otherwise you need to stick with the regular default PHP text. So why am I saying that this is a bade idea to create these kinds of texts. Well, it's specifically because they're enabled in the PHP INI file.
one of the wonderful things about PHP is that the code is portable. A version of PHP on Windows and MAC and Linux is all exactly the same. My code can just run on one, it can be moved to the other one and run their just as easily. But, if I make a choice to use a different style of tag and I require that the php.ini file be configured a certain way, then my code stops being portable. Now, it will no longer run on this other system because it's not configured correctly and I think that's a bad thing. If we just stick to the default standard tags, then our code will stay portable.
And that's going to be especially true if you start writing code that's going to be a plug in for existing application like Wordpress or Drupal or something. You want to be able to distribute that and have everyone be able to use it. So you want to be in the habit of using good, widely available, widely supported PHP tags. Otherwise, I think you're shooting yourself in the foot before you've even started. There are a couple of other points that I want to make about your php code. If we open up our myphpinfo.php file, I want to show you that the white space doesn't matter. White space inside the PHP code doesn't matter.
This is just as valid as what we had before. Same here, if we put more returns in here, tabs, spaces, those are all considered white space. And PHP doesn't care about those. It completely ignores it. It just executes the code as it goes through. And that's great, because it allows us, then, to use white space to help us to create readable code. We can indent things, we can group things together so that it's really nice and easy for us to read and follow. We don't have to worry how the white space is going to be interpreted. Not all languages work that way, and that's one of the nice things about PHP. The other thing I want to point out to you is notice that the command ends with a semicolon. That's part of the way that PHP helps to know when one command is over and another one is starting.
Since white space doesn't matter. It needs to have some reliable way to know this command is finished and I'm ready to move on to the next command. So every line is going to need to end in a semicolon. So get used to it. It's a habit you're going to have to get in, when working with PHP is to always put semicolons at the end of your lines. And there's one last thing I want to show you before we move on. Let's just close this up, we'll save it and close it. And inside our index.html file that we created earlier, if you don't have it, you can just create one, a nice, simple file inside here, index.html. Let's open that up, and let's just drop a little bit of PHP in there.
So open our PHP tags. You see that it auto closed it for me as well at the same time, that's nice. Phpinfo with my parentheses and then the semicolon, right? So that's the same little bit of PHP code that I had. Let's save it. We'll close that up. And let's now go into our browser and let's load that file up. Localhost, Kevin Scopeland. Index.html, that's the file I'm loading up. I get hello from my user directory, I did not get the result of that php function, it has nothing to do with the fact that there was other text in there at all.
In fact, if we view Source under web developer, I can choose, Page Source. And you can see that it actually just output the code itself. It didn't actually do any PHP processing on it. So why is that? I gave it my starting PHP tag and my ending PHP tag. Why didn't it process it? While that's because the file ends in .HTML. It's important that we have both of those things. The file needs to end in .PHP. That tells Apache, hey, be on the lookout for PHP tags. There may be PHP tags in this document.
And then when it sees a PHP tag It turns on it's PHP module. Starts processing the PHP until we close the PHP section. So it's important that we have both of these, right? If we start naming our files ending in something besides PHP, it's not going to see it as being a PHP file and try and do any processing on it. Now that we have the fundamentals of how to use PHP tags to embed PHP code into our document. Lets start trying to use that with dynamic text.
- 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 Basicswith 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?
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.