How to embed php code in a page

We#re going to start by learning how to embed PHP code in a page. Because there is some basic rules that we need to know about.



Notice that there is the beginning, and ending to this line with a bit of code in the middle.

I shall 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 are 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 are 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 that that is 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 it is 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 is turned on and off.

White Space in php


I want to show you that the white space does not 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 does not care about those.


It completely ignores it. It just executes the code as it goes through. And that is 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 is really nice and easy for us to read and follow. We do not 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 is part of the way that PHP helps to know when one command is over and another one is starting. Since white space does not matter.  It needs to have some reliable way to know this command is finished and I am 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 is a habit you are 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. Starts processing the PHP until we close the PHP section. So it is important that we have both of these, right? If we start naming our files ending in something besides PHP, it is 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.

Learn PHP 2016-07-21View: 1318

Categories: Learn PHP


Leave a comment