Creating Your First PHP Script

Posted on Jun 26, 2008 by Michael - PHP & MySQL

Here’s where all the action startscreating your first PHP scripts. With PHP, which will be run on the web server, you can make all kinds of things happen that couldn’t happen before. You can have your PHP retrieve data from databases, check someone’s password, print out customized greeting text, use cookies, write a guest book, create interactive games, calculate sales tax, or even build your own shopping cart or chat room web applications. And all these things can run by themselves, 24 hours a day, even while you’re peacefully sleeping in bed.

Our first PHP page is going to be a simple one, enclosing a simple PHP script. You can mix PHP and HTML in the same web page, but you’ve got to have some way to keep them apart. To do that, you enclose your PHP inside special tags <?php and ?>:

<?php
.
. Your PHP goes here....
.
?>

Actually, you can shorten this even more if you turn on short tags in php.ini, the PHP configuration file, in which case you can enclose your PHP between <? and ?>. I don’t recommend doing that, however, because that usage often means scripts can be used on other web servers beside one you’ve specially configured, and you may run into conflicts with other scripting languages.

Inside these tags, PHP scripts are made up of PHP statements, which end with a semicolon. The semicolon is important because it tells PHP when the current line is ended, and PHP will complain if you don’t use it. You may have seen other scripting languages, such as JavaScript, where the semicolons were optional at the ends of lines, but in PHP, they’re required.

Our first PHP script is the standard onewe’ll use a single statement here, just phpinfo(). As we’re going to see, PHP statements can do all kinds of things. This one uses the phpinfo function to display information about the version of PHP you’re using. A function is a set of statementssometimes hundreds of themthat are given a convenient name, such as phpinfo, by which you can invoke them. When you call such a function by name, all the statements in the function are run. The phpinfo function is one of the many functions built into PHP, ready for us to use. Because hundreds of lines are wrapped up into a single function, all we have to do is call that function by name.

In this case, the phpinfo function will create an HTML table holding information about your PHP installation. Here’s how we call this function in our script’s single statement:

<?php
phpinfo();
?>

Enter this PHP script in your text editor now, as shown in WordPad in Figure 1-2, and save this file as phpinfo.php. Don’t forget to make sure you’re saving this file as plain textif it’s not plain text, PHP will have a problem with it (one way to check is by typing out the file to take a look at what’s in it, such as by using the type command in a DOS window in Windows).

Figure 1-2. Creating a PHP script.

Congratulations, you’ve created your first PHP script! Not bad. Now that you’ve created phpinfo.php, the next step is to store it in the web server where that server can read it. If you’re working with an ISP, upload phpinfo.php to where you store your standard web pages on your ISP as you’d normally upload a web page, using an FTP application or web page interface.

If you’re working locally and have PHP and a web server already installed on your own machine, store phpinfo.php where the server can find it. In Apache, it’s htdocs in the directory where Apache has been installed. For IIS, it’s inetpub/wwwroot. In Linux, it may be /var/www/html. After making sure your web server has been configured to work with PHP, as detailed in the PHP installation directions from www.php.net, start your web server.

Running Your First PHP Script

To run phpinfo.php, just open it in your browser as you would a standard web page that you’ve uploadedthat is, navigate in a browser to the URL for phpinfo.php, such as http://www.yourisp.com/username/phpinfo.php. If you’re using PHP locally, navigate to http://localhost/phpinfo.php after starting your local web server.

Do not open phpinfo.php directly in your browser using a menu item such as File > Open because that would open the file without running it through the web server, so the PHP script wouldn’t run. Make sure you enter the appropriate URL in the browser’s navigation bar instead.

If all is well, you should see a table packed full of information about PHP, as it appears in Figure 1-3 in the browser. Congratulations, you’re a PHP developer! There are a number of tables here; it’s worthwhile scrolling down to take a look at the information on your PHP configuration. If you’re ever in doubt about what your PHP version has installed, this is a good place to check.

Figure 1-3. Results of the first PHP script.

What If It Doesn’t Work?

Unfortunately, plenty of things can go wrong when you first try to get PHP running. If things aren’t working right, don’t panic; it’ll just take a little extra time. You may get a blank page, a file-not-found error, or another kind of error, but the problem can be tracked down.

The first thing to check is whether PHP is running, which, if you’re using PHP locally, is easy to check. At the command prompt, change directories to the PHP install directory and run php -v. If the PHP is version displayed, PHP is running. If you can open a command prompt on your ISP using a Telnet or SSH2 application, you can run the same test.

The next item, and the most common issue, is that PHP may not have been installed correctly as far as your server is concerned. This is the problem if you get a blank page, and when you do a “view source” in the web browser, you can see the source code of your PHP script. This means that the web server did not pass the script to PHP to be run. This can be a little finicky, which is why the instructions from www.php.net are so extensive. The best idea is to go through those directions again, line by line, to make sure you did everything just as it’s listed.

Then make sure that phpinfo.php is where your web server expects to find it. As mentioned earlier, in Apache, this location is htdocs in the directory where Apache has been installed. For IIS, it’s inetpub/wwwroot. In Linux, it may be /var/www/html. The actual directory may be different on various servers; on one PHP server I use, the correct directory is /httpdocs/ROOT, so ask your ISP’s tech support. If you’ve uploaded phpinfo.php to the usual directory on your ISP for your HTML pages and it’s not working, ask your ISP’s tech support; sometimes they have to enable support on a directory-by-directory basis. For that matter, some ISPs require that you use a different extension for PHP 5 scripts, such as .php5.

Do you need any special Unix file protection level for PHP scripts? Must they be set as executables? No, simple 644 protection (not 755) will do.

If you’re using IIS, also check php.ini for the line cgi.force_redirect = 0, which must be set as indicated in the installation directions. If you don’t see it there, add it.

Finally, take a look at the “Problems?” section in the installation instructions for a troubleshooting guide. The PHP Frequently Asked Questions (FAQ) is at www.php.net/FAQ.php, and it handles many such problems, as does the PHP installation Frequently Asked Questions (FAQ) at www.php.net/manual/faq.installation. You might also check the PHP install archives at http://marc.theaimsgroup.com/?l=php-install&r=1&w=2, or the news groups alt.php or comp.lang.php.