Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > INTRODUCTION TO PHP


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Introduction to PHP
By Joe "Zonker" Brockmeier - 2003-12-29 Page:  1 2 3 4

Using PHP

Okay, so now you're convinced that you want to actually try PHP out. We'll walk through a few simple examples so you can get your feet wet. Keep in mind that this is by no means an exhaustive look at PHP, just a quick starter.

"Hello, World!"
To get a feel for PHP, let's look at some very simple PHP scripts. Since "Hello, World!" is an obligatory example, we'll produce a friendly little "Hello, World!" script.

As mentioned earlier, PHP is embedded in HTML. (You could have a file that contains almost no HTML, but usually it's a mixture.) That means that in amongst your normal HTML (or XHTML if you're cutting-edge) you'll have PHP statements like this:


<body bgcolor="white">
  <strong>How to say "Hello, World!"</strong>
           <?php echo "Hello, World!";?>

  <br>
  Simple, huh?
</body>

Simple, right? Just an "echo" statement, and that's it. Of course, that on its own isn't terribly useful. But it does teach us something about the language. (By the way, if you examine the HTML output, you'll notice that the PHP code is not present in the file sent from the server to your Web browser. All of the PHP present in the Web page is processed and stripped from the page; the only thing returned to the client from the Web server is pure HTML output.)

Printing date and time in a Web page
Now we'll do something a little more useful. This example will print out the date and time in a Web page.


<body bgcolor="white">

  <strong>An Example of PHP in Action</strong>
  
<?php echo "The Current Date and Time is:<br>";
         echo date("g:i A l, F j Y.");?>

// g = the hour, in 12-hour format
// i = minutes
// A = print AM or PM, depending...
// l = print the day of the week
// F = print the month
// j = print the day of the month
// Y = print the year - all four digits  

This code produces the output: The Current Date and Time is: 11:00 AM Friday, October 20 2000.

Notice the blend of PHP and HTML here. I'll assume that you can already read HTML, so I'll only explain the PHP code. You can find a complete PHP reference online at PHP.net (see Resources).

The PHP code begins with the tag <?php and ends with ?>. This tells the server that everything between <?php and ?> needs to be parsed for PHP instructions, and that if they're found, they need to be executed. Note that when your document is processed and served, it will be received by the client as plain HTML. Someone browsing your site will not see any of your PHP instructions, unless you've made an error and the server spits them out as-is instead of processing them first.

Regular HTML tags within the <?php and ?> will be processed normally. Note that the simple script above contains a <br> line-break tag. PHP wouldn't be very useful if you couldn't include HTML formatting as well.

If you're going to be working with others, or if you're just plain forgetful like me, you'll want to comment your code as well. The // characters indicate a comment, which the server will not process or pass on to the client, unlike comments in HTML. If you include a standard <!-- comment --> within the <?php and ?> tags, it is likely to cause an error when parsed by the server. Obviously, you probably wouldn't comment your code quite so much as I have above for such a basic function, but it makes a good example.

Finally, note that each PHP function is enclosed in parentheses and ends with a semicolon, which will seem familiar to fans of C or Perl. It's not uncommon to forget a closing parenthesis or semicolon and have a number of parse errors just because of a simple typo, so be sure to check them. It's helpful to edit PHP in an editor like Vim or Emacs that is capable of syntax highlighting. This allows you to catch your errors right away.

The date function is just one of the built-in PHP functions. PHP also comes with functions for database connectivity, creating PDF, Shockwave, JPG, GIF, PNG, and other graphics files, sending e-mail, reading and writing files, parsing XML, session handling, talking directly to the browser via HTTP, and many other functions.

PHP also allows the user to define their own functions. This makes PHP a language capable of providing a huge number of solutions via the Web. Rather than just writing everything from scratch, however, be sure to check sites like Zend.com, PHP Wizards, and, of course, Freshmeat to see if what you're trying to do has been done already (see Resources).

There are a lot of open sourced PHP solutions for serving banners, automating news sites, Web-based e-mail clients, database management, and much more. There's no sense re-inventing the wheel. Instead, start from the foundation that has already been built and customize it into your own solution. If you're just poking around with PHP to learn it and don't have a specific project in mind, these projects are still great examples of what you can do with PHP and serve as great learning resources.

View Introduction to PHP Discussion

Page:  1 2 3 4 Next Page: Summary and Resources

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.