Monday, September 27, 2010

Your First Php Script

Let's jump straight in with a PHP script. To begin, open your favorite text editor. Like
HTML documents, PHP files are made up of plain text. You can create them with any
text editor, such as Notepad on Windows, Simple Text and BBEdit on MacOS, or VI
and Emacs on UNIX operating systems. Most popular HTML editors provide at least
some support for PHP.
Type in the example in Listing 3.1 and save the file, calling it something like
first.php.

1: <?php
2: print "Hello Web!";
3: ?>
The extension to the PHP document is important because it tells the server to treat
the file as PHP code and invoke the interpreter. The default PHP extension for a PHP
4 document is .php. This can be changed, however, by altering the server's
configuration. You saw how to do this in Hour 2, "Installing PHP."
If you are not working directly on the machine that will be serving your PHP script,
you will probably need to use an FTP client, such as WS-FTP for Windows or Fetch for
MacOS to upload your saved document to the server.
After the document is in place, you should be able to access it via your browser. If
all has gone well, you should see the script's output. If PHP is not installed on your server or your file's extension is not recognized, you
may not see the output shown in Figure 3.2. In these cases, you probably will see
the source code created in Listing 3.1. Figure 3.3 shows what happens when an
unknown extension is encountered.If this happens, first check the extension with which you saved your PHP script. In
Figure 3.3, the document was accidentally called first.nphp. If the file extension is
as it should be, you may need to check that PHP has been installed properly and that
your server is configured to work with the extension that you have used for your
script. You can read more about installing and configuring PHP in Hour 2.
Now that you have uploaded and tested your script, you can take a look at the code
in a little more detail.

No comments:

Post a Comment