Sunday, August 17, 2008

Learning PHP

I spent some time this weekend to start learning PHP.  I browsed the web for good starter links for creating dynamic web pages with MySQL.  Obviously, the starting point was downloading and installing Apache, PHP5, and MySQL.  The installation went smoothly, I chose the Windows binary zip packages for PHP, and ran the MSI installs for Apache and MySQL.  After configuring Apache for PHP, and making minor modifications to the PHP.ini file, I ran my first PHP page (<?php phpinfo(); ?>).  This gave me a nicely formatted page that showed the PHP installation options and verified that everything was working.

Figuring that learning a new language from scratch may present some challenges, I started searching for a PHP editor that might ease the learning curve a little.  After searching for a while, I decided on Eclipse with the PDT.  I downloaded Eclipse, and then the Java 1.5 JRE, and then extracted the PDT binaries into the Eclipse folder structure and fired it up.

Eclipse is a very nice IDE that supports lots of interesting extensions.  The PDT module added on nicely and provided the option to create a PHP project, configure debuging with xdebug, and provided a very helpful panel (in the PHP perspective) called "PHP Functions" that allowed me to search for classes and functions available in PHP.

After overcoming the installation and configuration hurdle, I was ready to create my first *real* PHP application.  But, what would be a good application to start with?  I decided to create a simple contact management web application that would contain name, photo, e-mail, and phone number.  This application should introduce me to MySQL add/update/delete, dynamic tables/forms, postbacks, redirection, session/post/get variables,  file upload, sending image data from the server, and more.

I laid out the application in 3 pages:

Main Page
The main page will show the list of all contacts, it will display all contacts in the database, and access an images table via an imageid field in the contact row.
Add/Edit page
The add/edit page will allow creation and editing of the name, e-mail, phone, and also allow a photo to be uploaded for the contact.
Image retrieval page
The image retrieval page will read an imageid from the query string on the url and lookup the image data in the database, and send the image data back as an http response.

I created the tables in the database, and started coding away!  Some of the issues that I had to learn/overcome:
  • String concatenation (. instead of +)
  • Handling/determining postbacks (looking for hidden field or valid $_POST array values)
  • How to redirect the browser (setting the HTTP header before any other output)
  • Global variables (defining and using)
  • Configuring the xdebug debugger in Eclipse (minor config file tweaks)
  • Handling file uploads (PHP provides a lot of help with this!)
  • Handling images (PHP provides a lot of help with this!)
  • Accessing a MySQL database (intuitive implementation)
  • MySQL BLOB size limitations

After about 4-5 total hours...  I had a working application that allowed the addition/editing of contacts information, photo uploads in main, add, and edit pages, and display of photos in main page and edit page.  Knowing ASP/ASP.NET helped with some of the concepts, but overall PHP provides A LOT of functionality that helps out with common and advanced web application development.  I know that I have just scratched the surface with this application, but I have learned enough to know that I will continue developing with PHP!

I have already started researching the WS02 Web Services Framework for PHP.  They have a complete WS-* implementation for PHP that looks very interesting!



Blogged with the Flock Browser

0 Comments:

Post a Comment

<< Home