Slashdot Mirror


PHP Cookbook

CEHT writes "Like Tony Williams said in his review on PHP and MySQL Web Development: "PHP and MySQL are probably the most pervasive add-ons to Apache web servers across the web". And I agree with him. PHP is a very powerful scripting language, so developers (not just web developers) can do almost anything with it." Read on to see how well CEHT thinks O'Reilly's PHP Cookbook helps you do that almost everything. PHP Cookbook author David Sklar and Adam Trachtenberg pages 608 publisher O'Reilly rating 9 reviewer Edmond Lau ISBN 1565926811 summary Solutions and examples for PHP programmers.

The approach that the authors use in PHP Cookbook is great. Like most computer books, the authors usually include a summary (in sentence forms) to illustrate what the readers will expect in each chapter. Skalar and Trachtenberg take this even further by including some preliminary (code) examples to explain the general ideas behind each chapters. The examples in the book are self-contained. In most cases, I've found examples to exactly fit my needs -- this makes it one of the better reference books.

Each chapter in the book is divided into multiple sections of Problem / Solution / Discussion with a FAQ style. In each case, a simple description of a problem is followed by a PHP script as the solution. But the meat is actually in the discussions: in-depth details are included here, where the authors also include references, extended ideas, and scripts to inform the readers how much more they can do about the issue.

For example, I was going to add a simple script to my website to parse RSS/RDF files from certain news websites (CNN, Slashdot, ...), and use it as my Mozilla homepage. (Who wouldn't?) This script seems to be simple, but I may make a mistake here and there. As reference, I opened up the book to the section "Parsing XML with SAX." Then I realized the authors already had the script to parse RSS/RDF files in the discussion. Bravo!

For myself, the most useful chapters I found are: Web Basics, Forms, Database Access, and XML. There are also good examples in topics such as security, internationalization, and file processing/management. However, this book does not cover the basics of PHP. If you are a good programmer, you should be able to get away with this using the PHP Manual. A good book to learn PHP is Programming PHP, also by O'Reilly.

Although this book covers a wide range of topics, it does not cover topics like generating PDFs. I would also like to see the authors add one (maybe two) case studies in later editions. That would give the reader a more concrete example of how to combine tricks presented by this book. Other than that, at the price of $39.95 (or $61.95 CAD), this book is a great buy!

Topics

  1. Strings
  2. Numbers
  3. Dates and Times
  4. Arrays
  5. Variables
  6. Functions
  7. Classes and Objects
  8. Web Basics - available online as example chapter
  9. Forms
  10. Database Access
  11. Web Automation
  12. XML
  13. Regular Expressions
  14. Encryption and Security
  15. Graphics
  16. Internationalization and Localization
  17. Internet Services
  18. Files
  19. Directories
  20. Client-Side PHP
  21. PEAR

You can purchase the PHP Cookbook from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

25 of 238 comments (clear)

  1. Another book with similar title by bloxnet · · Score: 5, Informative

    Not to be confused with "PHP Developer's Cookbook" (ISBN: 0672323257 - Publisher: Pearson Education) which is a very well regarded reference for PHP. Just wanted to avoid confusion and suggest the book at the same time. There seems to be a flood of PHP/MySQL books out there, or people are just getting around to reviewing them....not sure.

  2. Re:PDFs? by Mr+Guy · · Score: 2, Informative

    Also, someone could bring this up, but I'd still like an answer. Isn't PDF owned by Adobe?

  3. Re:PDFs? by Anonymous Coward · · Score: 5, Informative

    From the adobe website:
    "An open file format specification, PDF is available to anyone who wants to develop tools to create, view, or manipulate PDF documents."

  4. Re:PDFs? by Anonymous Coward · · Score: 3, Informative

    Here's what I use http://www.fpdf.org/ It's a steep learning curve, but it works

  5. No, PDF is open... by SlashChick · · Score: 4, Informative

    The actual document specification for PDF is open. Adobe just happened to not only create the format, but also make the most popular PDF reader and writer (the Acrobat series.) There are, however, free alternatives such as Ghostscript that generate great PDFs from a Postscript file (which you can generate from any program in Windows just by checking the "Print to File" box on a Postscript printer.)

    If you're interested in generating PDFs from PHP, there are a myriad of options available by searching Google. Some web hosting companies also support generating PDFs from PHP, which makes generating PDFs a cinch.

    HTH!

  6. Re:PDFs? by holovaty · · Score: 3, Informative

    Yes, it's completely possible, as long as your version of PHP has pdflib [http://pdflib.com/] installed as a module or compiled in.

    And, yes, it appears to be legal. (It better be, considering I use it at my job.)

  7. Re:YES!!! by Surak · · Score: 4, Informative

    ... does anyone really know the original intent of perl? I guess that's like trying to find the oringal intent for ducK tape (and no, don't say to seal off ventilation ducts, coz that's what they'd have you believe.)?

    Duck tape was invented during WWII by Johnson+Johnson for the purpose keeping water out of ammunition cases -- it was relatively waterproof, hence the reason people in the military started calling it "duck tape."

    You should try watching the History Channel more often. ;)

  8. The perfect companion for this book... by jpkunst · · Score: 3, Informative

    ... is (IMHO) the MySQL Cookbook. The 'Cookbook'-format (specific real-world problems and possible solutions) makes for extremely useful books.

    JP

  9. Re:PDFs? by Kamel+Jockey · · Score: 3, Informative

    Can you generate a PDF with PHP without also generating an Adobe lawsuit?

    There's no liability shield built in, but FPDF is a great tool that can generate PDFs using PHP without the need for using a commercially-licensed (read "expensive") PDF generating library. I like it because it is distributed under a BSD-like license.

    --
    In case of fire, do not use elevator. Use water!
  10. Re:PHP Security by ikewillis · · Score: 2, Informative
    I'm working on a project that aims to solve the matter of keeping database access passwords secure when using PHP as an Apache module (and consequently all PHP scripts must be readable by a single user)

    It's called phpkeychain and it's modelled after the keychain mechanism in OS X.

  11. Re:Things PHP is missing by tkiehne · · Score: 2, Informative

    The PEAR packages, usually bundled with the distribution, would take care of the first two:
    http://pear.php.net/

    --
    -- t_kiehne
  12. Re:Things PHP is missing by weston · · Score: 4, Informative

    1) consistant database integration - Why not have a SetDBType() function, rather than hardcoding mysql_connect, mssql_connect, myodbc_connect, pgqsl_connect, etc?

    I believe there is a PEAR package that abstracts an OO layer over the functions to various databases. Think DBI for PHP.

    2) Native XML support - It's just not there? Why re-invent the wheel each time? Give us a good XML tree-walking engine DAMMIT!

    Natvie XML support is there. A SAX parser is usually built in by default. I'm happily using a DOM XML interface that I compiled into my version.

    3) sane and consistant functions. Single quotes, double quotes, some functions work with both, some work with one or the other, embedded html in an echo screws up if you don't double quote it, etc.

    Quotes in PHP do work in a sane and consistent way, very similar to the way they work in PERL. The only thing I mourn is the lack of a qq[-like facility (and I wouldn't be surprised if someone snuck it in while I wasn't looking, and I just didn't know about it).

    4) .NET support?

    Um.... why?

  13. Re:Enough MySQL by Kz · · Score: 2, Informative

    Because it's easy, fast, and 'real' enough for the vast majority of quick webpages.

    just like PHP

    if you want a big, complex, very scalable website; it might be better to go with java, for everything else (>90% of cases, i'm sure) it's easier to do it with PHP.

    just the same; if you want a heavy duty database, with lots of concurrent updates, and/or triggers, stored procedures and so... well you can do it with Postgres or mortgage your house and use Oracle. for everything else, it's easier, quicker; and usually faster to use MySQL

    --
    -Kz-
  14. Re:Things PHP is missing by Angry+White+Guy · · Score: 2, Informative

    It's also on the query side as well. How many people have written wrapper classes to deal with different databases?

    As a side effect, implementing one database call, even with a db_type parameter in the database calls, gives a certain level of abstraction which would allow for greater cross portability. It also helps with application debugging, generating the error closer to the actual failure point, rather than in the wrapper class itself.

    --
    You think that I'm crazy, you should see this guy!
  15. Excellent book, I heartily recommend it to any... by Naum · · Score: 3, Informative
    ...PHP developer.

    Relevant, real-life useful examples are given and even a seasoned pro like me picked up a few gems like the example user authentication code that utilizes a hash instead of having to go back to the database on each page fetch.

    My bookshelves are full of PHP books but most of them are inferior to the online documentation at php.net. They add nothing and are a true waste of trees. This one, however, does not fall into that category.

    --

    AZspot
  16. Re:Ain't php great? by Christianfreak · · Score: 2, Informative

    It would take a whole lot of perl code to achieve the same functionality that can be accomplished in 200 well-written php code.

    You obviously don't know Perl, at the very least you don't know about CPAN. How about a decent HTML parser for PHP. XML? How about this: write a program that reads an Excel spreadsheet and uploads its content into a database and let me know how long it takes you in PHP.

    PHP sucks because there is no standard way to extend it, and don't mention PEAR as it so poorly documented its almost completely unusable save for a few of the 'modules' that are there.

    Rather than implement proper namespaces the developers chose to throw everything in the core language. Thus you get a_function_for_everything() which sure makes it easy for the web monkeys to memorize it or look it up but never how or why something works, and in the end you get nothing more than an unmaintainable mess. What happens when a new feature is needed. Re-write city (Yes that is a rant because I've had live in the shadow of my web-monkey predecessor).

    Also what's with mixing data and logic? PHP's whole premise is just plain backwards. Just recently are they trying to bolt on some sort of templating.

    Other problems: no access to the web server, difficult to generate web-server response codes. Errors, including full path-names etc are spit out to the user. (great for security).

    Its been my experience in dealing with both PHP and Perl that proper, templated, documented code takes about the same amount of time and code. And with all the hacks to work around the above problems with PHP, its just easier and more maintainable to use Perl. (Too bad my employers don't see it that way)

  17. Re:Speaking of FUD by runderwo · · Score: 2, Informative
    ASP is not a language. PHP is a language (I'm being generous here).
    PHP actually is not a language. PHP is an engine for embedded scripting languages in markup documents. Zend is the scripting language that most people generalize as "PHP", but any other scripting language could also be developed and used with the PHP engine.

    Remember, PHP is to ASP as Zend is to VBScript.

  18. Re:Sick and tired of this by Anonymous Coward · · Score: 1, Informative

    I've never read a decent book on PHP, tho the manual is superb for basic stuff. I also want a book that covers things not so well documented online, that I'd otherwise have to grep the source files to learn. MCAL, MCRYPT, Berkley DB extensions etc are not covered well in the online manual. Yeah some advanced GD, curses and PHP-Gtk stuff would rock too.

  19. Re:Ain't php great? by Mr+Bill · · Score: 3, Informative
    It would take a whole lot of perl code to achieve the same functionality that can be accomplished in 200 well-written php code. (Depending on what it did - it's based off my personal experiences).

    If that is a challenge, then I accept. I can guarantee you that for every command in PHP, there is an equivalent command (or module) in Perl. Hence you should be able to write this imaginary program in exactly the same number of lines...

    Although perl and cgi scripts can be ran from the command line, they can't have (X)HTML mixed in quite as easily.

    Have a look at Embperl, HTML::Mason, Apache::ASP for a couple of examples of how to do this. Most people who knock perl have never discovered the right tools. Personally I would never use those tools, because I am a strong believer in the separation of Code and HTML. I use a templating system for all my projects, and you will never see one HTML tag in my code. Once you work with a team of developers and a separate team of designers you will understand the need for this separation.

    PHP is a great tool! I have used it for several applications in the past (6 or 7 years ago). But if you think that PHP can do more than perl can then you are mistaken...

  20. Re:The superiority of PHP over Perl by lamz · · Score: 3, Informative

    Amen!

    I did a lot of Perl development before switching to PHP, and some of the things you mention that are missing from Perl are readily available as modules from CPAN. But that just adds another point in PHP's favour -- the default install comes with all the stuff you mentioned.

    Before anyone gets their Perl backs up, let me point out why a good default install is important for web development: you don't always have control over the server, so you can't always get the Perl modules you want. (But if you want to re-write your code, sometimes, you'll get what you need! (Couldn't resist.))

    --

    Mike van Lammeren
    It will challenge your head, your brain, and your mind.

  21. Re:Hmm by waynetv · · Score: 2, Informative

    PHP has reflection...

    An entire chapter in the manual for it.

  22. Re:Speaking of FUD by lamz · · Score: 4, Informative

    PHP actually is not a language. PHP is an engine for embedded scripting languages in markup documents. Zend is the scripting language that most people generalize as "PHP", but any other scripting language could also be developed and used with the PHP engine.

    You have that ass-backwards. PHP is the language, Zend, the engine. Any other scripting language could be developed and used with the Zend engine.

    --

    Mike van Lammeren
    It will challenge your head, your brain, and your mind.

  23. Definitely Worth the Buy by GreyLurk · · Score: 2, Informative

    I've been using this book for about 3 months, and I'm no Newbie to PHP programming but it's got some invaluable stuff in there.

  24. Re:The superiority of PHP over Perl by Anonymous Coward · · Score: 1, Informative

    1. php + Smarty looks like cgi to me
    2. who in their right mind would *want* to mix html with their code? When I was handed a vendor's solution with php + html mixed in a single file the first
    thing I did was separate the two. How the hell can you see what is going on?
    3. so much of php looks like it was lifted right from perl.
    4. the real die hards are on a crusade to re-write the whole world in php. gotta laugh.
    5. how come just about every site, every book on php is geared towards amatuers? I can never find any depth. Even the manual on php.net has big holes in it.
    6. why are there 4 versions of the split function?
    7. as for all options being compiled in, they aren't. Did you ever try to build php for the first time? You'll spend hours trying to figure out which options you should include.

  25. Obligatory Smarty plug by mlas · · Score: 2, Informative

    1 - It lets you mix html and code in. Sure, it's not the only language that lets you do it, but it's still a strength.

    I thought that PHP did a great job of mixing HTML and code until my PHP apps started reaching a certain size. Even using function calls and classes, templates started getting convoluted. Then I discovered Smarty and my whole way of working changed. Smarty is a template manager that allows for more precise separation of content and code. As a super-simple example, lines like become {$variable}. Over the course of a whole site, the savings from this syntax alone ease code readability (yes i care about such a thing). But there's a lot more to smarty than shorter variables, including shorthand for string modifiers and loops, as well as a nifty caching system that can speed up dynamically generated sites, and zillions of other really cool things for "lazy" programmers like me.

    A lot of people on this thread have mentioned AdoDB (my other favorite PHP add-on), but I haven't seen anyone give a proper shout out to Smarty. It's made my life easier.

    --
    "Luck is the residue of design" --Branch Rickey