The PHP Extension and Application Repository, or PEAR for short, contains code written by other programmers to achieve various goals. The concept is simple: other programmers, usually specialists in their areas, write very complicated code, and present it to you in an easy-to-use form, thereby enabling you to create very powerful scripts using just a few simple commands.
PEAR contains two types of pre-written code: PECL code, and PHP code. PECL code, as mentioned already, forms full extensions written in C that interact with external libraries. Extensions reside in PECL when they are considered useful, but not popular or much used. Most of PEAR, however, is PHP code that performs all varieties of tasks for you, with the primary advantage being that you can use PEAR code on any PHP server without needing to enable any extensions or re-compile PHP.
The most famous package in PEAR is called PEAR::DB, and provides an object-oriented, database-independent framework for reading and writing to your database. PEAR::DB is covered in depth later on.
PHP comes with "go-pear", an easy way to configure PEAR for use on your computer. To use it, simply run go-pear from the command line and follow the on-screen instructions. Windows users will need to change directory to where PHP is, e.g. c:\php.
Once you have PEAR installed on your system, you will see the "pear" command - this allows you to search for and download new PEAR modules for your PHP installation.
Author's Note: I've had people email me asking why my code examples rarely make use of PEAR - sometimes I write things from scratch rather than using the pre-built PEAR code. Well, there are two reasons for this: first, if I teach you how to use PEAR, you won't learn how to do it yourself. This is not necessarily a bad thing; certainly I wouldn't want to try re-implementing parts of C++'s Standard Template Library! However I think there's a lot to be learnt by writing things yourself.
The second - and important - reason is that PEAR isn't installed on that many web servers, or if it is installed it may not be kept up to date. So if I had used PEAR in my code examples, many thousands of you wouldn't be able to use them because your host provider doesn't support it! That, in a nutshell, is why I haven't used PEAR very much - sorry!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Running PHP scripts >>
Previous chapter: Extending PHP
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.