Docstoc

Chap01

Document Sample
Chap01 Powered By Docstoc
					                                                                                                 1
                                                              What Is PHP?

The World Wide Web has changed very fast in so many ways. Sometimes it seems like yesterday that a
little known markup language with a strange name HTML (Hyper Text Markup Language) was used by
some physicists to link scientific documents at a group of CERN servers. It was wondrous to read some
text somewhere in the world with just a simple program, and what is more information in the document
could magically transport you to another one with related information.

And this spread relatively quickly to other sciences. Text-only interfaces were the norm, and simplicity
of accessing information content the most important part of the equation. Text documents with a small
set of tags and a simple server setup was all you needed to inform your colleagues and share the
knowledge, independently of whether the organic chemist at the other end was using his trusty Mac, or
the theoretician was using her Unix box, or the impoverished graduate was using a second or third
hand PC running very flaky TCP/IP software.

Nowadays we expect more, much more than this. We expect a web site with lots of information, and a
good presentation, but we do not want to be distracted by a difficult interface. The information should
be easy to find, and it should be current. A clean and dynamic web site is a great asset for the user and
for the information provider. Long gone (fortunately) are the days of garish-looking web sites with
blinky thingies, lots of animated images that usually were hiding a shallow content depth. We want
information, we want it 5 minutes ago and we want it in the way we like it.

A modern web site is not just a web server; it also includes a way of storing data and querying (a SQL
database perhaps), a way of processing the requests from the user and creating documents with the
appropriate information. Many are the options open to the web developer, but not all of them as open
and general as others. We should not only consider the immediate task at hand of creating a site with
dynamic content, we need to be sure that we can still be providing the said content independently of
the changes in hardware or software technology.

We want to try and insure ourselves against future technology changes, dramatically reduce our license
costs, keep our hardware budget under control, and yet be portable to different web servers and
operating systems. We also want some assurance that we can do something about that killer bug we
just think we found in our web server or scripting environment, be able to understand (if we want to)




                               TEAM FLY PRESENTS
 how the scripting works, and be able to modify the behavior of our web server or scripting host to meat
 some particularly unusual need. Open source products will be your best assurance that your application
 that works now in the "Super-Turbo Hexium IX" machine of today, will work in the "Nanotech Cube
 Aleph" of tomorrow (I am exaggerating just a wee bit).


Enter PHP
 PHP (acronym for: PHP Hypertext Preprocessor), is a server-side embedded scripting language. This
 means that it works within an HTML document to confer to it the capacity of generating content on
 demand. You can convert your site into a web application, not just a collection of static pages with
 information that may not get updated quite so often, which may be alright for a "personal" web site
 (yes, we all have made such a beast), but not for one that is going to be used for business or for
 education.

 You may be asking "But, why PHP? There are so many other options like ASP, Cold Fusion, Perl,
 Java, Python, even good old shell/awk/sed scripts?", and the answer will be: simplicity, an almost
 natural way of using databases and platform independence.

 And did I mention it was open source?

 Of course general scripting or programming languages like Perl, Python, etc. have also platform
 independence, and are open source. They are great languages, and sometimes an overkill for what you
 need, like using a concrete mixer to make scrambled eggs. PHP was designed to work on the web, and
 in this ambit it excels; connecting and querying a database is a simple task that can be handled in 2 or
 3 lines of code. The PHP scripting engine is well optimized for the response times needed on web
 applications, it can even be part of the web server itself improving the throughput even more.

 If it were only a matter of improving the speed of the scripts, then PHP will be one of many solutions.
 But there is more to the PHP equation than that. There is the simplicity and robustness of the language
 and the scripting engine. There is the connectivity to an ever increasing number of database servers,
 the shorter development cycles and the ease (encouraged by the syntaxes and constructs) of creating
 modular and reusable components.

 You can perform tasks as simple as creating a feedback form that sends an e-mail to the web
 maintainer, to a whole database driven document management system (like Midgard,
 http://www.midgard-project.org/), to helpdesk or bug tracking systems (like Keystone,
 http://www.stonekeep.com/keystone.php3), to a shopping cart application (like
 FishCartSQL, http://www.fni.com/fcsql/), to what would be considered "middle-ware"
 packages without the need for extra languages or frameworks, and whole libraries for quick and
 flexible development (PHPLIB, http://phplib.netuse.de/).

 Then there is the support from a widely distributed and cooperative community, with several source
 repositories (like PHP Code Exchange, http://px.sklar.com/ or Berber's WeberDev
 http://www.weberdev.com/), many sites with tutorials (PHPBuilder,
 http://www.phpbuilder.com/; PHPWizard, http://www.phpwizard.net/, WebMonkey,
 etc.) and thriving (high volume) mailing lists.

 And did I mention that it is open source?

 There’s no more waiting until the next release for a feature to be added or a bug to get fixed. Just take
 the source, make your modifications and there you are, instant customization and complete control. No
 more guessing at whether a particular function or feature is insecure, the code does not lie. And who




                                TEAM FLY PRESENTS
  knows, maybe your modification gets to be so popular that others may want to use it (hey! instant
  fame). And you cannot beat the total price for a development environment using the combination of
  Linux, Apache, MySQL and PHP, not only cheaper than other more proprietary environments, but also
  more stable and robust. As Eric Raymond said "given enough eyes, all bugs are shallow".

It All Began...
  Some time in 1994 when Rasmus Lerdorf put together a bunch of Perl scripts to track down who was
  looking at his resume. Little by little, people started to get interested in the scripts, and they were later
  released as a package "Personal Home Page" tools (the first meaning of PHP). In view of the interest,
  he wrote a scripting engine and incorporated another tool to parse input from HTML forms: FI, Form
  Interpreter, thus creating what was called variously PHP/FI or PHP2. This was done around mid 1995.

  Soon, people started to use these tools to do more complicated things, and the development changed
  from just one person, to a group of core developers in charge of the project and its organization. This
  was the beginning of PHP3. This group of developers (Rasmus Lerdorf, Andi Gutmans, Zeev Suraski,
  Stig Bakken, Shane Caraveo, and Jim Winstead), improved and extended the scripting engine and
  added a simple API that will allow other programmers the liberty to add more functionality to the
  language by writing modules for it. The language's syntax was also refined, with constructs that will be
  familiar for people coming from object oriented or procedural languages. If you know C, C++ or Java,
  or have done even some shell/awk scripting, or written a Pascal or VBasic program, learning the basic
  PHP constructs will be a breeze.

  The PHP language features the usual complement of control structures, operators, variable types,
  function declarations and class/object declarations that we have been accustomed to expect from any
  compiled or interpreted language, and yet it also has features of its own. For example, in C you employ
  pointers, in other scripting languages this can be cumbersome or even not possible, but in PHP this is
  just one use of variable variables (discussed in detail later in the book), as the code below shows:

      $peru = array("domain"=>"pe", "capital"=>"Lima");
      $japan = array("domain"=>"jp", "capital"=>"Tokyo");

      function show ( $country ) {
         echo "Internet domain = ".${$country}["domain"]."\n";
         echo "Capital city = ".${$country}["capital"]."\n";
      }

      show ("peru");

      // Prints:
      // Internet domain = pe
      // Capital city = Lima

      show ("japan");

      // Prints:
      // Internet domain = jp
      // Capital city = Tokyo

  The trick is in the ${$country}[] call, which in turn is equivalent to using $peru[] or
  $japan[] . But that is not all, what if you want to make a function that uses another one for
  comparing a couple of items. An idea similar to the one shown above allows us to pass different
  comparison functions in the main function parameters:




                                  TEAM FLY PRESENTS
      function bigger($x, $y, $comp_func) {
         if ( $comp_func($x, $y) ) {
            $out = "Item ".$x." is bigger than ".$y;
         } else {
            $out = "Item ".$x." is not bigger than ".$y;
         }
         return $out;
      }

      bigger(2, 1, "num_comp");
      // will use the function num_comp() to compare the numbers

      bigger("epsilon", "gamma", "greek_comp");
      // will compare the strings as names of greek alphabet characters

  In this way making general handling routines is simpler, and no, there is no typo above, it is
  $comp_func and not $$comp_func , think a little about it and you will see why.

  Don’t worry if much of the above doesn’t make sense at this moment. These examples are just a taste
  of what is to come in the rest of the book and will be fully explained.

And the Current Situation is...
  PHP version 4 (PHP4), based on the Zend engine (details at http://www.zend.com/). This
  scripting engine has been designed from the ground up to be easily embeddable in different
  applications. PHP4 is the first application using the Zend engine, but it could also be included in other
  packages, for example in MySQL (which could be a good way to enable stored procedures in that
  database).

  There is already a beta version of PHP4 (beta 3 when this book was written). I will recommend a visit
  to the Zend web site for more information; particularly tantalizing is the possibility of using COM and
  perhaps CORBA with this engine.

  It is also easy to note a trend towards more and more sites using PHP for their scripting needs. The
  statistics (available at the main PHP site's usage page, http://www.php.net/usage.php3;
  courtesy of Netcraft, http://www.netcraft.com/), show a continuous increase in the total
  number of domains and IPs using PHP as an internal Apache module (about 1,000,000 virtual servers).
  There is also a good number of sites using PHP as a stand-alone module and these will not show in the
  surveys that Netcraft conducts.

  Notable is also that mod_PHP (as the corresponding Apache module is called) is the most popular
  module for the most popular web server in the Internet (E-Soft Inc.'s web survey, http://www.e-
  softinc.com/survey/). Even a cautious forecast will predict a steady increase of the usage of
  PHP, even more when the final PHP4 version appears with all the promise that the Zend engine holds.


Book Style and Organization
  The book will emphasize clarity over conceptual profundity and practical real-world examples over
  abstract examples (such as "Hello World" and "$foobar = 1"). Our aim is to present code that is useful
  (with little modification) to the reader, not intricate technical discussions. These examples will try to
  be as web browser-neutral as possible, which is a good strategy for any robust web application. Use of




                                 TEAM FLY PRESENTS
 platform specific client browser features will be avoided, e.g. if JavaScript is generated, this will be
 constructed so it will work well independently of whether the user runs Internet Explorer under
 Windows or Netscape Navigator on a Solaris box.

 Although PHP development requires the use of other technologies such as HTML, SQL, and HTTP
 servers, this book will not attempt to be a full resource on those subject areas. Technologies other than
 PHP will be addressed only in the context of their use and interaction with PHP. For example, we will
 not discuss at length the basis of XML and its specification. We will, however, demonstrate how to use
 MySQL and PHP together to build a web database application.

 We divide this book into a number of sections. The first section introduces the basics of the language
 with clear and "real life" examples, that is we tried to avoid making "Hello World"-like scripts, after
 all you want to develop solutions for a web application. The second section will contain chapters
 showing more complex use of PHP functions (databases, image creation, etc.). The third section will
 deal with fully discussed applications (such as a shopping cart application). And finally we are
 including a section with appendices containing a general reference on the language, as well as
 discussions on the open source concept and similar topics.


In Closing
 This book will cover the core language of PHP, including issues such as installation and configuration,
 and demonstrations of the practical use of the language. The focus is on common business needs, such
 as database application development and e-commerce. Explicit mention will be made of the pros and
 cons of certain approaches, and the trade-offs involved. In the end, our aim is to provide you with a
 new and powerful tool, so that you, the web application developer, will be able to create a better
 project in a shorter period of time.

 If we at least convince you to consider PHP as a viable alternative for your projects, we will be happy.
 And even more, if you decide not only to think about using it, but actually go ahead and use it for real
 and then participate in the community of all the other PHP developers, that my friend will make us feel
 warm all over (as the saying goes). So, sit back, grab some coffee and enjoy.


Useful Websites
 Here is a preliminary list of websites you may like to visit to keep up-to-date with developments in
 PHP, Apache and MySQL. A fuller reference list is included in the appendices at the end of this book.

    ❑    PHP site: http://www.php.net/
    ❑    PHPBuilder site: http://www.phpbuilder.com/
    ❑    Apache Project site: http://www.apache.org/
    ❑    MySQL site: http://www.mysql.com/




                                TEAM FLY PRESENTS

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:4/11/2012
language:
pages:5