4.2 Setting Up an HTML/PHP Environment
4.2.2 Use an Editor to Create PHP Scripts
Just like HTML documents, PHP scripts are text files that can be created with any text editor. The AceHTML freeware editor used to produce all the code in this book3 provides convenient editing and color-coded syntax formatting capabilities for creating and editing PHP code just as it does for creating HTML documents.
At first, it may not be obvious that you cannot execute PHP scripts directly from an editor’s browser window, as you can with HTML docu- ments. You can create and edit PHP scripts with an editor, but you must then execute them on a server regardless of whether you are using a remote server or a server residing on your own computer. For example, on a Windows computer with a default WAMP installation, PHP files are probably saved in the C:\wamp\www folder and executed by entering localhost\{PHPfile name}in a browser window. It shouldn’t make any difference which browser you using because any browser will display your HTML document and the execution of PHP scripts doesn’t depend on your browser.
The required folders for using PHP are automatically created when an Apace server is installed. For convenience, you can also store the corre- sponding HTML documents in the same folder. For example, Documents
3Visicom Media’s AceHTML freeware editor is no longer available, but a “Pro”version is available for purchase. There are several other freeware programming editors available online that are suitable for the purposes of this book.
70 4 Creating a PHP Environment
4.1a and 4.1b could both be stored in the C:\wamp\www folder on a Windows computer.
In summary, to execute a PHP application, create it in a code or text editor, save it inwww(for a WAMP installation) then switch to a browser to execute it at localhost on a local server or do whatever is required to execute it on a remote server. If the PHP application is accessed locally through an HTML document, then you will open that document in your browser at localhost\{HTML document name}. Whenever you make changes, save them and refresh the PHP or HTML document in your browser.
When you create applications in any language, it is important to develop a consistent approach that minimizes the time spent correcting the errors you will inevitably make. This is especially important when using PHP, which requires switching back and forth between your editor and (for a local server) your localhost.
It is rarely a good idea to create an entire application all at once. A much better plan is to proceed step-by-step, adding small sections of code and testing each addition to make sure the results are what you expect. When you pass information from an HTML document to a PHP application, it is always a good idea to display the values passed to the PHP application beforewriting more PHP code.
The error messages you receive when you make mistakes in your code will almost never be as helpful as you would like, although experience and practice will improve your ability to interpret these messages. They may tell you where an error has been encountered but not what (or even exactly where) the error actually is. You might like to see a message like “You forgot to put a semicolon at the end of line 17.” But that willnot happen!
PHP interpreters will never tell you what you really need to know—exactly what you did wrong and how tofix it. And of course, no syntax checker will protect you against the worst errors of all—code that works perfectly well but is logically flawed and gives the wrong answers!
The Apache server installation automatically creates a log file con- taining all PHP error messages, wamp\php_error.log on a Windows computer. It appends new error messages to the existing log so, over time, this file can become very large. You can delete this file at any time and it will be recreated the next time an error is encountered. For some kinds of errors, thefile can become large from just trying to execute a single PHP script.
To test your PHP/server environment, start with this minimal PHP script. Name it helloWorld.php and save it in wamp\www (or the
equivalent location on your system). Every PHP script is enclosed inside a
<?php…?>tag.
Document 4.2 (helloWorld.php)
<?php
echo"Hello, world!";
?>
Open a browser and type localhost\helloWorld.php (or whatever is the appropriate URL for your system). You should see the text, “Hello, world!” displayed in your browser window.
Your computer is probably configured to automatically associate HTMLfiles with an.htmor.htmlfile name extension with a browser. So, if you double-click on a file with such an extension, it will open in your browser.4But, if you double-click on afile with a.phpextension, the result is uncertain unless you have specifically associated the.phpextension with some other application, such as a code editor. If you haven’t made such an association, your computer may ask you to find an application for opening suchfiles. (You could use Notepadon a Windows computer, for example.) In any case, you cannot“execute”thisfile by double-clicking on it. Instead, you must enter its URL in a browser, as noted above.
You can also save and execute this file:
Document 4.3 (PHPInfo.php)
<?php
echo phpinfo();
?>
This file will display a great deal of information about how PHP is configured on your server. (If you view the source code for this document, you can also learn a lot about formatting output from PHP.)
The echo language construct in Documents 4.2 displays the specified text string enclosed in quote marks,"Hello, world!", and Document 4.3 displays the (very long!) string output returned byphpinfo().
4If you have more than one browser on your computer, it will be the default browser or one you have designated as the default.
72 4 Creating a PHP Environment
Even more simply, you can just type http://localhost/ (for a WAMP server). There should be an index.php file that will, by default, display some information about your server and PHP configuration.
The first thing to notice about Documents 4.2 and 4.3 is that the PHP files are not associated with an HTML document; they serve as stand-alone applications.
If you get error messages, or if nothing happens when you try to execute the scripts in this chapter, then something is wrong with your server/PHP installation. It is hopeless to try to offer system-specific advice for resolving this kind of problem, but the most likely sources of trouble at this level, assuming that you have installed both a server and PHP, is that some server configuration options have been overlooked or have been given inappro- priate settings, that you haven’t stored your PHP application in an appro- priate folder on a local computer, or that you do not understand the procedure for running PHP applications on a remote server. You may need to consult with your system administrator (if you have one) to resolve these problems.