Foreword


Think back to the late '80s and early '90s. The world was moving from dialup BBS systems to this new Internet thing. It was mostly limited to technical college students and eager hobbyists. Cool programs such as Gopher, Veronica, Archie, and IRC gave us a hint of what was to come.

In April of 1993, Mosaic started to pull things together, and when Netscape 1.0 was released in December of 1994, we were in high gear. Web servers were available from CERN and NCSA, and the recently released Perl 5 quickly emerged as the scripting language of choice for developing dynamic Web sites. The Internet ”through the sugarcoated pill of the World Wide Web ”was ready for the masses.

Writing Web applications in 1994 was an adventure. You either had to brave this weird Perl thing that was geared at system administrators who needed to manipulate large log files, or you had to write C programs that manipulated a lot of text and generated HTML output. Because Web programming involved parsing text input and generating text output, Perl was a much closer fit than C for most people.

For example, let's say we wanted to create a simple form that asked the user for her name and age and displayed it back to us when the submit button was clicked. Here is what you would need to do if you wanted to write this in C:

 #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define ishex(x) (((x) >= '0' && (x) <= '9')  ((x) >= 'a' && (x) <= 'f')  \                   ((x) >= 'A' && (x) <= 'F')) int htoi(char *s) {     int  value;     char c;     c = s[0];     if(isupper(c)) c = tolower(c);     value=(c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;     c = s[1];     if(isupper(c)) c = tolower(c);     value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;     return(value); } void main(int argc, char *argv[]) {     char *params, *data, *dest, *s, *tmp;     char *name, *age;     puts("Content-type: text/html\r\n");     puts("<html><header><title>Form Example</title></header>");     puts("<body><h1>My Example Form</h1>");     puts("<form action=\"form.cgi\" method=\"POST\">");     puts("Name: <input type=\"text\" name=\"name\"><p>");     puts("Age: <input type=\"text\" name=\"age\"><p>");     puts("<input type=\"submit\">");     puts("</form>");     data = getenv("QUERY_STRING");     params = data; dest = data;     while(*data) {         if(*data=='+') *dest=' ';         else if(*data == '%' && ishex(*(data+1)) && ishex(*(data+2))) {             *dest = (char) htoi(data + 1);             data+=2;         } else *dest = *data;         data++;         dest++;     }     *dest = ' 
 #include <stdio.h> #include <stdlib.h> #include < ctype .h> #include <string.h> #define ishex(x) (((x) >= '0' && (x) <= '9')  ((x) >= 'a' && (x) <= 'f')  \ ((x) >= 'A' && (x) <= 'F')) int htoi(char *s) { int value; char c; c = s[0]; if(isupper(c)) c = tolower (c); value=(c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16; c = s[1]; if( isupper (c)) c = tolower(c); value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10; return(value); } void main(int argc, char *argv[]) { char *params, *data, *dest, *s, *tmp; char *name, *age; puts("Content-type: text/html\r\n"); puts("<html><header><title>Form Example</title></header>"); puts("<body><h1>My Example Form</h1>"); puts("<form action=\"form.cgi\" method=\"POST\">"); puts("Name: <input type=\"text\" name=\"name\"><p>"); puts("Age: <input type=\"text\" name=\"age\"><p>"); puts("<input type=\"submit\">"); puts("</form>"); data = getenv("QUERY_STRING"); params = data; dest = data; while(*data) { if(*data=='+') *dest=' '; else if(*data == '%' && ishex(*(data+1)) && ishex(*(data+2))) { *dest = (char) htoi(data + 1); data+=2; } else *dest = *data; data++; dest++; } *dest = '\0'; s = strtok (params,"&"); do { tmp = strchr (s,'='); if(tmp) { *tmp = '\0'; if(!strcmp(s,"name")) name = tmp+1; else if(!strcmp(s,"age")) age = tmp+1; } } while(s=strtok(NULL,"&")); printf("Hi %s, you are %s years old\ n",name,age); puts("</body></html>"); } 
'; s = strtok(params,"&"); do { tmp = strchr(s,'='); if(tmp) { *tmp = '
 #include <stdio.h> #include <stdlib.h> #include < ctype .h> #include <string.h> #define ishex(x) (((x) >= '0' && (x) <= '9')  ((x) >= 'a' && (x) <= 'f')  \ ((x) >= 'A' && (x) <= 'F')) int htoi(char *s) { int value; char c; c = s[0]; if(isupper(c)) c = tolower (c); value=(c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16; c = s[1]; if( isupper (c)) c = tolower(c); value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10; return(value); } void main(int argc, char *argv[]) { char *params, *data, *dest, *s, *tmp; char *name, *age; puts("Content-type: text/html\r\n"); puts("<html><header><title>Form Example</title></header>"); puts("<body><h1>My Example Form</h1>"); puts("<form action=\"form.cgi\" method=\"POST\">"); puts("Name: <input type=\"text\" name=\"name\"><p>"); puts("Age: <input type=\"text\" name=\"age\"><p>"); puts("<input type=\"submit\">"); puts("</form>"); data = getenv("QUERY_STRING"); params = data; dest = data; while(*data) { if(*data=='+') *dest=' '; else if(*data == '%' && ishex(*(data+1)) && ishex(*(data+2))) { *dest = (char) htoi(data + 1); data+=2; } else *dest = *data; data++; dest++; } *dest = '\0'; s = strtok (params,"&"); do { tmp = strchr (s,'='); if(tmp) { *tmp = '\0'; if(!strcmp(s,"name")) name = tmp+1; else if(!strcmp(s,"age")) age = tmp+1; } } while(s=strtok(NULL,"&")); printf("Hi %s, you are %s years old\ n",name,age); puts("</body></html>"); } 
'; if(!strcmp(s,"name")) name = tmp+1; else if(!strcmp(s,"age")) age = tmp+1; } } while(s=strtok(NULL,"&")); printf("Hi %s, you are %s years old\ n",name,age); puts("</body></html>"); }

For a coder , this wasn't actually so bad, except when a simple change to the HTML was needed. In that case, you had to recompile the CGI program. This is why Perl caught on: Not only is it much easier to write the code to deal with our simple form, but you don't need to compile Perl scripts. The same form in Perl using CGI.pm is

 use CGI qw(:standard); print header; print start_html('Form Example'),     h1('My Example Form'),     start_form,     "Name: ", textfield('name'),     p,     "Age: ", textfield('age'),     p,     submit,     end_form; if(param()) {     print "Hi ",em(param('name')),         "You are ",em(param('age')),         " years old"; } print end_html; 

This is infinitely easier both to write and to read. But again, I am talking from a coder's perspective. As the Web caught on, the number of non-coders creating Web content grew exponentially. In the beginning, they cared only about static content and spent all their time learning the intricacies of HTML. But soon they were asked to add dynamic content to their sites. Writing CGI programs in C was out of the question for most of them. The brave ones dove into Perl and mastered the art of CGI.pm and the various other helper modules available for Perl.

This is where PHP found its niche. I would love to say today that I saw everything this clearly and thought up PHP to fit perfectly into this role. That wasn't quite the case. I found the overhead of launching a Perl interpreter for every request much too large. I also found writing CGI programs in C much too tedious . But I had written all sorts of CGI programs in C, and found that I was writing the same code over and over. What I needed was a simple wrapper that would enable me to separate the HTML portion of my CGI scripts from my C code so that I could change the HTML without needing to recompile the C code. This concept became PHP, and the previous C and Perl examples ended up looking like this in PHP:

 <html><header><title>Form Example</title></header> <body><h1>My Example Form</h1> <form action="form.phtml" method="POST"> Name: <input type="text" name="name"> Age: <input type="text" name="age"> </form> <?if($name):> Hi <?echo $name?>, you are <?echo $age?> years old <?endif?> </body></html> 

Compared to the C and Perl versions of the example, this is very HTML-centric. This was, and still is, what attracted many people to PHP. They didn't want to learn how to program, they just wanted to make their HTML pages more dynamic.

PHP has grown as a language over the years, and it is now very much a real programming language. But it has not lost its capability to enable people to get simple things done extremely quickly. It is still very friendly to non-programmers. In a way, PHP is a sneaky programming language in that it enables people to get things done without any programming experience, but as they work with it, they slowly teach themselves to program without realizing it.

PHP is very easy to use, and you should be able to get far by yourself. But it is sometimes interesting to see how other people have solved both simple and complex problems. There are times when you won't be able to come up with the one slick way of solving a problem. That is where this book comes in. Sterling and Andrei have put together a cookbook of solutions that shows you how to solve specific problems and at the same time provides you with the fundamentals required to solve almost any problem.

Sterling joined the PHP development team in early 2000 and he has contributed the Curl, SWF, and Sablotron extensions. Andrei has been a driving force behind the development of PHP since early 1999. His direct contributions have been mainly in the areas of Perl-style regular expressions, XML-based features such as WDDX, session support, and general-purpose array manipulation functions.

If you enjoy this book and you enjoy working with PHP, consider becoming involved with the development of PHP just as Sterling and Andrei did. It is fun and very rewarding to contribute to a popular Open Source project. The work you do is used by hundreds of thousands of people and the respect you earn from your peers in the industry is priceless. Most importantly, you can help make sure that free and open software continues to advance. New people and new ideas are the driving forces behind projects such as PHP.

- Rasmus Lerdorf (rasmus@php.net)



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

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