HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [195]
So, to serve a file from the server, it must be located in the htdocs directory of your server install. If you’ve installed XAMPP, go to the folder where you installed XAMPP (probably either c:/xampp or c:/Program Files/xampp) and locate the htdocs directory. This is where you’ll put all your PHP files. Make note of it now.
If you’re using a remote server, you’ll need to use your hosts file management tools or FTP (both described in Book VIII, Chapter 1) to transfer the file. Normally, you can place your files anywhere on the remote file system.
To get the hang of placing your files in the correct place and accessing them, create a test file that will display all your PHP, Apache, and MySQL settings.
To test everything, make the PHP version of the famous “Hello World!” program. Follow these steps to make your first PHP program:
1. Open a text editor to create a new file.
PHP files are essentially plain text files, just like XHTML and JavaScript. You can use the same editors to create them.
2. Build a standard Web page.
Generally, your PHP pages start out as standard Web pages, using your basic XHTML template. However, start with a simpler example, so you can begin with an empty text file.
3. Add a PHP reference.
Write a tag to indicate PHP. The starting tag looks like . As far as XHTML is concerned, all the PHP code is embedded in a single XHTML tag.
4. Write a single line of PHP code.
You’ll learn a lot more PHP soon, but one command is especially useful for testing your configuration to see how it works. Type the line phpinfo();. This powerful command supplies a huge amount of diagnostic information.
5. Save the file to your server.
A PHP file can’t be stored just anywhere. You need to place it under an accessible directory of your Web server. If you’re running XAMPP, that’s the htdocs directory of your xampp directory. If you’re running a remote server, you’ll need to move the file to that server, either with your host’s file transfer mechanism, an FTP program, or automatically through your editor. (See the nearby sidebar “Picking a PHP editor” for information on remote editing in Komodo.)
6. Preview your page in the browser.
Use your Web browser to look at the resulting page. Note that you cannot simply load the file through the file menu or drag it to your browser. If you have XAMPP installed, you need to refer to the file as http://localhost/fileName.php. If the file is on a remote server, use the full address of the file on that server: for example, http://myhost.freehostia.com/fileName.php.
Your code from Steps 3 and 4 should look like this:
phpinfo();
?>
Hmm. Only three lines of code, and it doesn’t seem to do much. There’s precious little HTML code there. Run it through the browser, though, and you’ll see the page shown in Figure 1-1.
If you see the actual PHP code rather than the results shown in Figure 1-1, you probably didn’t refer to the page correctly. Please check the following:
♦ Is the file in the right place? Your file must be in htdocs or on a remote server (or in a subdirectory of these places).
♦ Did you use the .php extension? The server won’t invoke PHP unless the filename has a .php extension.
♦ Did you refer to the file correctly? If the URL in the address bar reads file://, then you bypassed the server, and PHP was not activated. Your address must begin with http://. Either use http://localhost (for a locally stored file in XAMPP) or the URL of your remote hosting service.
This phpinfo page of Figure 1-1 is critical in inspecting your server configuration. It displays all the different settings for your server, describing what version of PHP is running and what modules are active. This can be very useful information.
Figure 1-1: That tiny PHP program sure puts a lot of information on the