by Sandra Henry-Stocker
Internet

Discovering PHP

November 5, 2008, 02:04 PM — 

If you've never used PHP, you might be very surprised to learn how easy it is to embed PHP commands in HTML files. The files don't require execute privilege and don't have to be stored in cgi-bin directory to work. In addition, reading and using data from forms is significantly easier since PHP stuffs the data into arrays. The $_POST[name] field, for example, would hold text entered into the "name" field in a form when the form uses the POST method to send the data. The $_GET array provides the same functionality for data sent using the GET method. If you want to write a script that is independent of the method -- no problem: use $_REQUEST. This array contains the elements from the $_GET and $_POST along with the contents of $_FILES, $_COOKIE, $_SERVER and $_ENV as well. What you don't ahve to worry about when using PHP is, therefore, how to read from standard in or the QUERY_STRING variable, how to break the data into a series of "parameter=value" pairs, how to break the parameter/value pairs apart and then unencode their values. PHP takes care of all of this for you.

The only downside that I see to PHP is that it's not likely to be installed on your systems unless you make it so. When I installed it on a Solaris 9 box, I also had to install about eight additional packages and do some very modest reconfiguration of my Apache server to get it working.

PHP has a look which reminds one of C and Perl. That is, it assigns variables using a $name=value; syntax, uses // or /* and */ for comments and includes a wide set of functions, array capabilities and other high level language functionality. But just look how easily you can set up a form and send email in a single file:




<?php
if (isset($_REQUEST['email']))
//if form is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail( "myself@example.com", "Subject: $subject",
  $message, "From: $email" );
  echo "Thank you.  Your request has been sent.";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='tryme.php'>
  Your Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Please explain your concern:<br />
  <textarea name='message' rows='10' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>



If you'd like a gentle but very effective introduction to PHP, I highly recommend the tutorial available at the W3Schools URL:

http://www.w3schools.com/php/default.asp

Once you've gotten your feet wet by wading through the example scripts, you might be ready to delve into O'Reilly's PHP Cookbook. The second edition of this task-oriented text contains hundreds of extremely useful code examples. All laid out in a problem, solution and discussion format, they provide code that you can use right out of the book or tailor to your needs.

The PHP Cookbook is in its 2nd edition, was published in 2006 and was written by David Sklar and Adam Trachtenberg. It's a getting stuff done by example book, but it's also a very serious programming text. The book contains an impressive amount of code, addresses some very practical applications and is very well thought out and organized.

Like other books in the cookbook series, the PHP Cookbook makes it easy for you to pick up the book with a particular task in mind and find just the right section to help you implement your code. You don't have to wade through a lot of material just to determine the context in which the answers are being provided.

O'Reilly also offers a Learning PHP 5 book for those not ready for the cookbook (which assumes a good amount of programming expertise), an "in a Nutshell" book and several other books, including two that address working with both PHP and MySQL.

I like it!
Comments

Easy AND Powerful

Good article. PHP has been around for a while and I came to it only recently to do a project. Having heard about it for several years before and not fully understanding its great power and flexibility. I love its integration with databases.

I have not found any web programming "problem" that can't be addressed using PHP.

Cheers PHP!
| reply
Free books

Build your tech library with our book giveaways.

Hacking Exposed, Sixth Edition
By Stuart McClure, Joel Scambray, George Kurtz; Published by McGraw-Hill/Osborne

The original Hacking Exposed authors rejoin forces on this tenth anniversary edition to offer completely up-to-date coverage of today's most devastating hacks and how to prevent them. Using their proven methodology, the authors reveal how to locate and patch system vulnerabilities. The book includes new coverage of ISO images, wireless and RFID attacks, Web 2.0 vulnerabilities, anonymous hacking tools, Ubuntu, Windows Server 2008, mobile devices, and more. Enter now!

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace