Chapter 17:
Creating Web Pages With PHP

Google

How to create a web page: Start with HTML

It's very simple to create a page that your script can display in a Web browser. I'm going to assume you know how to make HTML files. Here are the steps you should follow:

Compose the page in your favorite HTML editor. Be sure the file name ends in ".htm" or ".html." View it as a local file in your browser. If you're using Windows, open the Windows Explorer and locate the file. Double-click the file name. It will open in Internet Explorer. Edit the page until it looks just right. Each time you make a change, click the "Reload" button in the browser to see the new version.

For industrial-strength web design, I strongly recommend Macromedia's Dreamweaver MX ( http://www.macromedia.com/software/dreamweaver ). It has a very rich feature set and makes many tasks a lot easier. It also produces much "cleaner" code than most other editors. It's currently priced at $399. However, if you're a student or a teacher, you can get it for $99 (not licensed for commercial use; be careful not to violate the license agreement).

If you want to save money or just want an editor that's very easy to learn and use, try Edit Plus ( http://www.editplus.com/ ). You can try it out for free and it's only $30 to buy. It's great for writing code in PHP, ASP, Perl, C/C++, Java, JavaScript and VBScript. I use it for all my programming.

With any browser (I.E., Firefox, Netscape, etc.), you can load the file by typing its full path name (beginning with "file:///") in the address bar of the browser window. I strongly recommend that you look at the page in all browsers to be sure that it will display correctly for the majority of users. Some extra effort at this stage will prevent users of one browser or the other from seeing a page that doesn't look right to them.

Be aware that some HTML editors optimize code for a certain browser. Some tags are not interpreted the same way by different browsers. To look good to the largest possible audience, try to avoid using code that is not compatible with both of the "big two." Keep in mind that most people are using a resolution of 800x600 pixels. You should also check to see that it looks right to all those millions of AOL users.

Note: AOL's browser is based on I.E. If something looks good in I.E. it'll also look good on AOL - most of the time. If you don't have an AOL account, use www.anybrowser.com to check the appearance of your page.

Tip: It's certainly true that more people use Internet Explorer than Firefox or Netscape. This is partly because the browser came with their Windows installation. Just be aware that many people who use I.E. at home surf the net on their lunch hour at the office and they may be using Netscape. In many corporate environments Netscape rules! These lunchtime surfers may be financially well-off and thus be good prospective customers. Don't let a sloppy page cost you sales and profits.

Now, remove any blank lines from the .htm or .html file. This is very important because a script may process the web page code differently than a browser does. Sometimes it will matter and sometimes it won't. Just make this a habit and avoid all the hassles. Here's a very simple HTML file to illustrate my point:

<html>
<head>
<title>My First PHP Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div align="center">
<H3>Hello World, I'm Steve Humphrey<br>This is my first web page</H3>
<H3>Today's Date is: April 25, 2004</H3>
<H3>There's not much here but that's OK for now</H3>
</div>
</body>
</html>

Save this code as file1.html and upload it to your server. Now open your browser and type "http://www.mysite.com/file1.html" in the address bar. Remember to replace "mysite.com" with the actual domain name of your site.

Now make it a PHP file: Just change the filename from file1.html to file1.php. You don't need to change anything else yet. It's now a PHP file. Upload it to your server and type its URL into the address bar of your browser. See? It loads and displays exactly as it did before.

Want to show the date and time automatically? That's easy! Add this block of PHP code at the top of the file:

<?php $date = date("F dS, Y h:i:s"); ?>

Next, find this line in the file:

<H3>Today's Date is: April 25, 2004 11:33:05</H3>

Change it to:

<H3>Today's Date is: <? echo $date; ?></H3>

Save the file and upload it to your server. Call it from your browser. You'll see that now it shows the correct date. It will always show the correct date and time when it loads because $date is assigned the value that the date( ) function got from the web server's system clock.

There's a chart on the next page that explains all the formatting codes you can use with the date() function to control how the time and date are displayed.

Previous Page   Table of Contents   Next Page

Copyright © 2004 Steve Humphrey