Basic Syntax


As I mentioned in the book's introduction, PHP is an HTML-embedded scripting language. What HTML-embedded means is that you can intermingle PHP and HTML code within the same script.

To begin programming with PHP, start with a simple Web page. Script 1.1 gives an example of a no-frills XHTML Transitional document, which I'll be using as the foundation for every Web page in the book (see Appendix C, "Resources," for information on XHTML).

Script 1.1. A basic XHTML 1.0 Transitional Web document.


To place PHP code within this document, you surround the code with PHP tags, either the formal and preferred

 <?php ?> 

or the informal

 <? ?> 

Anything placed within these tags will be treated by the Web server as PHP (meaning the PHP interpreter will process the code; text outside of the PHP tags is immediately sent to the Web browser).

A final consideration for your PHP scripts is that the file must use the proper extension. The extension tells the server to treat the script as a PHP page. Most Web servers will use .html or .htm for standard HTML pages, and normally, .php is preferred for your PHP scripts.

To make a basic PHP script

1.

Create a new document in your text editor (Script 1.2).

Script 1.2. This first PHP script doesn't do anything, per se, but does demonstrate the syntax to be used.


It generally does not matter what text editor you use, be it BBEdit on the Macintosh, the very basic Notepad or more advanced Dreamweaver on Windows, or vi on Linux.

2.

Start with your HTML document.

 <!DOCTYPE html PUBLIC "-//W3C//  DTD XHTML 1.0 Transitional//EN "http://www.w3.org/TR/xhtml1/DTD/  xhtml1-transitional.dtd> <html xmlns="http://www.w3.org/1999/  xhtml xml:lang="en" lang="en"> <head>   <meta http-equiv="content-type"    content="text/html;    charset=iso-8859-1 />   <title>Basic PHP Page</title> </head> <body> <p>This is standard HTML.</p> </body> </html> 

Although this is the syntax I'll be using throughout the book, you can change the HTML to match whichever standard you intend to use (e.g., HTML 4.0 Strict).

3.

Before the closing body tag, insert your PHP tags.

 <?php ?> 

It is highly recommended that PHP developers use the formal PHP tags, and I'll do so in this book. These are also known as XML-style tags.

4.

Save the file as first.php.

Remember that if you don't save the file using an appropriate PHP extension, the script will not execute properly.

5.

Place the file in the proper directory of your Web server.

If you are running PHP on your own computer (presumably after following the installation directions in Appendix A, "Installation"), you just need to move, copy, or save the file to a specific folder on your computer. You can check the documentation for your particular Web server application to find this directory. Common options are C:\inetpub\wwwroot (IIS on Windows), C:\Program Files\Apache Group\Apache\htdocs (Apache on Windows), or ~/Sites (Mac OS X with Apache, where ~ refers to your Home directory).

If you are running PHP on a hosted server (on a remote computer), you'll need to use an FTP application to upload the file to the proper directory. Your hosting company will provide you with access and the other necessary information.

6.

Run first.php in your Web browser (Figure 1.1).

Figure 1.1. While it seems like any other (simple) HTML page, this is in fact a PHP script and the basis for the rest of the examples in the book.


Again, if you are running PHP on your own computer, you'll need to go to http://localhost/first.php or http://localhost/~<username>/first.php (on Mac OS X, inserting your actual username). If you are using a Web host for your domain name, you'll need to use http://your-domain-name/first.php.

You should see a simple, but perfectly valid, Web page. If you see the PHP code (the tags), then either you did not use the proper extension or PHP is not enabled on your server.

Tips

  • To find more information about HTML and XHTML, see Appendix C, "Resources," or check out Elizabeth Castro's excellent book HTML for the World Wide Web with XHTML and CSS: Visual QuickStart Guide (ISBN 0-321-13007-3).

  • One of the problems with using Notepad on Windowswhich is frankly not a very good text editoris that it will try to add .txt to the file's name when you save it. You can avoid this by saving the file as "first.php" (using the quotes) or by setting the Text Documents drop-down menu to All Files before saving.

  • There are actually four different pairs of PHP tags. Besides the two I've already mentioned, there are the ASP (<% and %>) and script styles (<script language="php"> and </script>), but these are far less commonly used.

  • If you are uncertain about the status of PHP on your Web server, you should run the phpinfo() example demonstrated in Appendix A, "Installation," before continuing through this chapter.

  • Because I am running Apache on my own computer within a small network, you will sometimes see URLs like http://192.168.0.101/ch01/first.php in this book, where 192.168.0.101 is the address of my server within this network. You'll see this in the occasional image, so don't let that confuse you.

  • You can embed multiple sections of PHP code within a single HTML document (i.e., you can go in and out of the two languages). You'll see examples of this throughout the book.

  • Because PHP scripts need to be parsed by the server, you absolutely must access PHP scripts via the URL (http://localhost/first.php or http://www.dmcinsights.com/first.php). You cannot simply open them in your Web browser as you would a file in other applications (in which case the address would start with file://).




    PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
    PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    ISBN: 0321336577
    EAN: 2147483647
    Year: 2005
    Pages: 166
    Authors: Larry Ullman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net