Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 93507 Viewers
82 Video lessons · 104378 Viewers
71 Video lessons · 76500 Viewers
56 Video lessons · 107821 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.