Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [196]

By Root 1447 0
screen.

You generally should not have a page with all the phpinfo() information running on a live server because it tells the bad guys information they might use to do mischief.

Picking a PHP editor

In the previous edition of this book, I recommend using Aptana for PHP editing. If you already use Aptana for your other Web editing, you may also enjoy using it for PHP. However, Aptana has changed, and PHP support is no longer built into the standard version of Aptana. If you want to have PHP support (syntax completions and the like), you need to install a plugin. Choose My Studio from the Window menu, click the Plugins tab, find the PHP plugin, and then click Get It. I’ve heard reports of the installation not working, but it worked fine for me.

I honestly prefer using Komodo Edit (also mentioned in Book I, Chapter 3) for PHP editing. It’s a little simpler than Aptana, and it still has all the important features like syntax completion and highlighting built in with no plugins needed.

Komodo has another feature that can be a lifesaver for PHP programmers. If you’re working on a remote Web server, you can set up a connection to that server (choose Edit⇒Preferences⇒Servers). Then you can use the Save Remotely command to save the file to the server directly. That way, you can use all the features of Komodo without a local installation of Apache or PHP, and without having to implement an extra file transfer step.

This test.php program shows one of the most interesting things about PHP. The program itself is just a few lines long, but when you run it, the result is a complex Web page. Take a look at the source of the Web page, and you’ll see a lot of code that you didn’t write. That’s the magic of PHP. You write a program, and it creates a Web page for you.

Don’t panic if you don’t understand the first thing in the page that gets produced with the phpinfo() command. It contains many details about how PHP is configured on your server, which may not mean much now. If you have trouble with PHP and ask me for help, though, it’s the first thing I’ll ask you for. As you get more experienced, you’ll be able to learn a lot from this page.

The basic flow of PHP programming works like this:

1. You build a standard page, and you include PHP code inside it.

2. When the server recognizes the PHP code, it calls the PHP interpreter and passes that code to it.

PHP programs are almost always designed to create HTML code, which gets passed back to the user. The user will never see PHP code, because it will get translated to HTML/XHTML before it gets to the browser.

By default, Apache will load index.html or index.php automatically if you type a directory path into the Web browser. There’s already a program in htdocs called index.php. Rename it index.php.off. Now, if you navigate to http://localhost/, you’ll see a list of directories and files your server can run, including test.php. When you have a live site, you’ll typically name one file index.html or index.php so the user doesn’t have to type the entire filename. See Book VIII, Chapter 1 for more information on how to set up your server to make it easiest to use.


Building XHTML with PHP

In PHP, you aren’t actually printing anything to the user. Instead, you’re building an HTML document that will be sent to the browser, which will interpret the HTML and then print that (the HTML) to the user. Therefore, all your code gets interpreted twice: first on the server to generate the HTML and then on the user’s machine to generate the output display.

If you’ve used XHTML, CSS, and JavaScript, you might have been frustrated because all these environments run on the client, and you have no control of the client environment. You don’t know what browser the user will have, and thus you don’t know exactly how XHTML, CSS, and JavaScript will run there. When you program in PHP, you’re working on a machine (the server) that you actually control. You know exactly what the server’s capabilities are because (in many cases) you configured it yourself.

It’s still not a perfect situation, though,

Return Main Page Previous Page Next Page

®Online Book Reader