Creating Your First PHP Script


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....         . ?> 

NOTE

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.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net