Well we’ve talked a lot about PHP, but how do we use it in practice?
That’s a great question! So let’s take a look at using PHP in HTML.
HTML, if you don’t know, is a way for marking up our text and presenting it in a fashionable way to a person surfing the web. In the end it is usually our goal to serve the surfer an “HTML” page. But what if you want your HTML page to change based on certain factors, such as the time of day, the particular viewer, etc. This is where PHP comes into play and we’ve seen this a bit in the previous lessons.
But how do we merge HTML and PHP? Well if you don’t know HTML I’ll try to give some explanation along the way but you might want to go learn it before moving on with this lesson. Here is an example HTML page:
<html> <head> <title>This is the title shown above the menu bar on my browser</title> </head> <body> I wish I knew how to write PHP in HTML files... </body> </html>
If you’re not a seasoned HTML guru I recommend copying the above code into a text file and naming that text file index.html. Now double click the file. It should open in your browser just like any other web page you might come across on the net. The only difference is no one else in the world can see it because the file is locally stored on your home computer. If double clicking the file didn’t work try dragging the file into your favorite browser or even open it from the browser menu if need be.
Enough talk let’s get to putting PHP in HTML already. Let’s take what we’ve learned in the previous lessons and just shove it right in there. Yes it is that simple
.
<html> <head> <title>This is the title shown above the menu bar on my browser</title> </head> <body> <?php print "I know how to write PHP!"; ?> <b>Perhaps I'll be lazy and write more php.</b> <?php print "No way! PHP is cool!<br />"; ?> </body> </html>
Well that’s about it. You can really shove PHP just about anywhere you want in an HTML file. When the user gets it they will have a pure HTML file with no PHP in it. This is because the website (server) will interpret the PHP before giving it to the person wanting to look at your HTML page.
Note: Since you don’t have a server to help interpret the PHP on your local machine the trick of double clicking the html file will no longer work. The HTML will be processed but the PHP will not since there is no interpreter on your local computer.
Back to Learn PHP in 24 Hours for more fun













Written by admin
Topics: Lessons