Thursday
PHP & MySQLCreating Your First PHP Script
Essential PHP
- Creating Your First PHP Script
- Running PHP on the Command Line
- Mixing PHP in Some HTML
- Creating PHP Variables

Here’s everywhere 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 take place that couldn’t take place 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, start 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 vacant 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....
.
?>

Really, you can shorten this even more if you turn on small tags in php.ini, the PHP configuration file, in which case you can enclose your PHP between <? and ?>. I don’t recommend doing that, but, because that treatment 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 vital because it tells PHP when the current line is finished, and PHP will complain if you don’t use it. You may have seen other scripting languages, such as JavaScript, everywhere 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 vacant to see, PHP statements can do all kinds of things. This one uses the phpinfo function to show 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 start an HTML table land 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. Making a PHP script.
Congratulations, you’ve made your first PHP script! Not terrible. Now that you’ve made phpinfo.php, the next step is to store it in the web server everywhere that server can read it. If you’re working with an ISP, upload phpinfo.php to everywhere you store your standard web pages on your ISP as you’d naturally 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 everywhere the server can find it. In Apache, it’s htdocs in the directory everywhere 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 meticulous 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 frankly 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 must 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! Here 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 excellent place to check.

Figure 1-3. Results of the first PHP script.
What If It Doesn’t Work?
Unfortunately, plenty of things can go incorrect when you first try to get PHP running. If things aren’t working right, don’t panic; it’ll just take a small 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 simple to check. At the command prompt, change directories to the PHP install directory and run php -v. If the PHP is version showed, 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 small finicky, which is why the instructions from www.php.net are so extensive. The best thought 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 everywhere your web server expects to find it. As mentioned earlier, in Apache, this location is htdocs in the directory everywhere 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 right directory is /httpdocs/ROOT, so question your ISP’s tech support. If you’ve uploaded phpinfo.php to the typical directory on your ISP for your HTML pages and it’s not working, question your ISP’s tech support; now and again 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 here, add it.
Finally, take a look at the “Problems?” part in the installation instructions for a troubleshooting guide. The PHP Frequently Questioned Questions (FAQ) is at www.php.net/FAQ.php, and it handles many such problems, as does the PHP installation Frequently Questioned 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.
Post Tags: Apache, Essential PHP, Install PHP Locally, Installing PHP, Komodo, Learn PHP, Maguma, Personal Homepage, Personal Web Server, PHP, PHP FAQ, PHP for Beginner, PHP for Newbie, PHPEdit, PWS, Running PHP, Running PHP Script, Zend Studio
Related Posts
- Creating PHP Variables
- Troys: One Twitter Script to Rule Them All
- Web Access with LWP
- Getting PHP
- Mixing PHP in Some HTML
- Gendai Games Launches GameSalad Beta
- Running PHP on the Command Line
- Constructing Fixed Layouts with a Simple CSS Framework
- Hollywood Studios Taking on Online Video
- Scripting a Common Browser Menu
Popular
- iPhone or iPod Touch, Which One Should You Choose? - 47,821 views
- Introduction to Facebook - 26,722 views
- 7 Top Twitter Topic Trackers - 16,420 views
- Introduction of Hacking Methodology - 15,070 views
- Google Hacking Keywords - 12,006 views
- LG BD390 Wi-Fi Blu-ray Player Review: So Packed You’ll Forget About Blu [Review] - 10,109 views
- BlackBerry Curve 3G: A Familiar, Powerful Phone - 9,838 views
- Complete Guide to Web Site Marketing - 9,805 views
- Microsoft’s Zune HD Wiill Have OLED, HD Radio - 9,319 views
- Photo Album and Printing Services - 8,978 views
Featured Articles
- The Elements of the Facebook Platform
- Introducing the Facebook Platform
- Social Skills Today Are Being Lost
- GibBook – Gibraltar Own Social Network Site
- 7 Deadly Twitter Sins
- Introduction to Facebook
- Google’s Wave: Many Online Apps in One Tool
- App Mapping War Casualties Debuts for Memorial Day Weekend
- Google@Omgili Mashes Traditional Web Search With Social Buzz
- Capital Factory: Austin-Based Incubator (RWS Interview)
Recent Posts
- Tweet via something
- Lightroom Review And Other Phot
- Several Words About The Advantages Of Tablet PCs
- Dissimilar Positive Features Of Table Computers
- Digi Link Doctor
- Why You Need Professional SEO Software
- Automated SEO Tools For Online Business
- How To Choose The Best Tablet PC
- Tablet PC – The Latest Computer Technology
- Tablet PCs Compared To Laptops