PHP Cookbook
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
- Strings
- Numbers
- Dates and Times
- Arrays
- Variables
- Functions
- Classes and Objects
- Web Basics - available online as example chapter
- Forms
- Database Access
- Web Automation
- XML
- Regular Expressions
- Encryption and Security
- Graphics
- Internationalization and Localization
- Internet Services
- Files
- Directories
- Client-Side PHP
- 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.
One thing I would like to see more PHP books do is to cover the various Security problems that are prevalent in many PHP based web applications.
Don't get me wrong, I find PHP to be the best and friendliest solution for many things, but some of the Security problems could easily be avoided with some common sense security advice.
In all seriousness I enjoy PHP because it is pretty self explanatory, and it can use plain old html inside it too. It's just nice to use a scripting language for the web that was made for webpages originally, not a language that was created for ...
Ignore the "p2p is theft" trolls, they're just uninformed
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.
Also, someone could bring this up, but I'd still like an answer. Isn't PDF owned by Adobe?
Never confuse volume with power.
"A teacher overheard him say that he was using PHP, and as part of our Zero-Tolerance policy against drug use, he was immediately suspended. No questions asked," said Principal Clyde Thurlow. "We're not quite sure what PHP is, but we suspect it may be a derivative of PCP, or maybe a new designer drug like GHB."
Parents are frightened by the discovery of this new menace in their children's school, and are demanding the school do something. "We heard that he found out about PHP at school on the internet. There may even be a PHP web ring operating on school grounds," said irate parent Carol Blessing. "School is supposed to be teaching our kids how to read and write. Not about dangerous drugs like PHP."
In response to parental demands the school has reconfigured its internet WatchDog software to block access to all internet sites mentioning PHP. Officials say this should prevent any other students from falling prey like Brett Tyson did. They have also stepped up locker searches and brought in drug sniffing dogs.
Interviews with students suggested that PHP use is wide spread around the school, but is particularly concentrated in the geeky nerd population. When contacted by BBspot.com, Brett Tyson said, "I don't know what the hell is going on dude, but this suspension gives me more time for fraggin'. Yee haw!"
PHP is a hypertext preprocessor, which sounds very dangerous. It is believed that many users started by using Perl and moved on to the more powerful PHP. For more information on how to recognize if your child may be using PHP please visit http://www.php.net.
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."
I've been doing PHP web development on and off for a couple of years now and I've always found that it's greatest strength has been the availability of very god online resources.
PHP.net and many other excellent resources are only a browser click away and remain up to date for free. PHP is one of those areas where I'll save my money and buy a book I'll get genuine reference use from.
Here's what I use http://www.fpdf.org/ It's a steep learning curve, but it works
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!
Simpli - Your source for San Jose dedicated servers and colocation!
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.)
I've got the Perl cookbook, and have used many of the source code samples found inside in my work. However, most of the development I do is PHP, and I'm extremely excited to hear that they've released a cookbook for PHP. I reccomend this and the Perl cookbook to any developer who wants advice or inspiration.
Matthew Walker
http://www.tweeterdiet.com/ - My Diet Tracking Tool
/. itself may not be, but PhpSlash is. Also, I suspect the reason the slashdot crowd likes PHP so much is due to (among other things) a couple of reasons:
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.
2 - It's free, open source (zend crap excluded)
3 - Microsoft hates it. That's always a plus.
4 - It's increadibly flexible and forgiving. Coming from a c background, this is one of it's most useful features to me. As far as I can tell, most programmers are inherantly lazy; They don't want to work harder than necessary to do a good job. In PHP, types are incredibly loose. If you want to use a string as a integer, a float, and a string all in the same line, you don't have to cast it. It just does it, and the incredibly vast majority of the time, it does just how you expected it to. Why can't C be that smart?
Contact Me (got tired of viruses emailing me).
1) consistant database integration - Why not have a SetDBType() function, rather than hardcoding mysql_connect, mssql_connect, myodbc_connect, pgqsl_connect, etc?
.NET support?
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!
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.
4)
In short, PHP is a good language for small projects, but just doesn't cut it in an enterprise setting.
I'm a PHP/MySQL junkie, and every time I see anything about PHP on
</rant>
Im dreaming ofa big bndwdth, That can resist the
I actually bought a copy of this book, as well as the other one referenced in this review. I found them quite useful in learning PHP - the examples can be easily tweaked to create useful little applications. I haven't done very much with web programming except for few CGI-scripts and PHP definitely looks much easier way to create useful applications.
The examples of integrating to MySQL were especially useful as I have been playing with Microsoft Access using MySQL as server, and now I can easily create web views from the database!
JP
This book has been out for nearly a year. Is this news?
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!
Since the "language of browsers" is ECMAscript it makes sense to use that on the server, too, so one doesn't have to constantly shift back and forth between languages.
That doesn't even remotely make sense. Most browsers have javascript built-in, so that should decide what language to use server-side? Personally, keeping javascript for client-side and PHP or whatever for server-side is less confusing. That contextual switch is easier when the client-side / server-side code looks different.
Now I'm off to find a review of a Javascript book, so that I can insert random comments about what a shitty little language it is. That will be quite helpful for someone who wants a book about Javascript, I'm sure.
Mike van Lammeren
It will challenge your head, your brain, and your mind.
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-
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
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)
The Anti-Blog
Remember, PHP is to ASP as Zend is to VBScript.
LRC, the best-read libertarian site on the web
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...
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...
*Obligatory PHP flame from the Perl Guy *
Of course unlike Perl there are no easy/good ways to create one in PHP
*/Obligatory PHP flame from the Perl Guy*
There are no good ways to create one in any language. Current solutions are either:
(1) A thin shell over text and graphics primitives
or (2) dependent on an external rendering engine which creates another format like Postscript.
I'd like a high-level PDF creation library that would let me, say, directly create a table that is sized to fit the contents like HTML, but unlike HTML, will split the table across multiple pages, repeating header and footer information. Sounds straightforward, right? Not possible now. I've checked with Adobe engineers, and it's not even possible with their $20K PDF generation toolkit.
I can just hear the XSL-FO people saying, "but it IS possible with XSL-FO." And it's also possible, given all the sand and steel you could possibly need, to build a window. Point is, it's currently way too much trouble. I'm sure it'll get there eventually, but PDF creation is really an idea in its infancy right now.
bp
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.
So what are you saying is better than PHP? Running ASP with Javascript (or ECMAscript to be pedantic)?
If you can tell me that handling forms and working with databases can be done better with something other than PHP please do.
Also, please tell us what is a 'bigger' language, if PHP and *gasp* Perl are so little?
I wonder because I started in ASP with VBScript. I learned Perl and PHP and now I do PHP pretty much full time. If I want a script that is blantent CGI I use Perl. To me PHP and Perl completely blow away (not blow chunks) ASP with VBScript -- they are far better languages for web development. I can't say exactly why I think that, but one of the main things for me is the quality of the community surrounding the language. There's a lot more user support for PHP and Perl which to me is more helpful than the MSDN library will ever be. I also like a lot of punctuation -- but that's just a personal preference.
As far as switching back and forth between languages (JavaScript and PHP, for example), I never thought it was so silly. To me, doing things client-side is distinctly different from doing things server-side. It's no problem to have different languages for those two things. Especially when have to limit what you do on the client side because every browser is different. It seems to me like wasted effort to spend to much time on JavaScript stuff, because lo and behold browser X won't support what I'm trying to do. If I do it server side in PHP, I have an easier time writing portable code.
You certainly have a right to advocate your language of choice, and probably a right to bash others, but could you explain yourself a little better?
My Karma was at 49, then they switched to words. All that work for nothing!
PHP has reflection...
An entire chapter in the manual for it.
to see a book that deals with advanced application design in PHP. The problem with PHP is that most PHP developers are amateurs who don't know the first thing about app design. At best, apps are written with objects used randomly to accomplish some tasks and mostly include files for 2 dozen reused functions (or functions that aren't even reused, but still included in every page).
At worst, every database connection is hard-coded in a different place and there are no comments anywhere.
In either case, my God help anyone who wants to add functionality. These same people need a good book on relational database design, or be subjected to 4 years of universigy RDBMS design courses like I was. IMO, if you aren't willing to slit your wrists to get out of an RDBMS design class, you haven't taken enough of them.
The global economy is a great thing until you feel it locally.
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.
rmstar
If you want to use a string as a integer, a float, and a string all in the same line, you don't have to cast it. ... Why can't C be that smart?
...one of the major drawbacks to that is processing overhead. In VB, any variable you don't define is of type "Variant", which is 14 bytes plus the actual data. Ouch. Not to mention the work that has to be done to figure out what the data is and properly deal with it.
:)
Having a pretty broad background in VisualBasic...
*puts on asbestos suit and goggles*
And, of course, making a typo in a variable name can make debugging a real headache. Because you don't have to cast your variables, anything you type is valid, and typos don't generate errors! (You'll always find "Option Explicit" in my VB modules!)
Having about a month's worth of self-taught PHP behind me, and actually being very comfortable and productive with it, I find that the no-cast variable system works very smoothly. It's a little wierd when it comes to arrays, but good naming conventions help there. It's nice to not have to stop and make a new variable declaration all the time - just code.
And let's not forget the true power of no-cast variables: obfuscation! He's using it as a flag! No wait.. it's a loop counter! NO! It's an array! AAAHH!!
=Smidge=
I think a large part of the problem is that PHP is illsuited for many of the larger web applications it is being used for. Traditional Application Server features such as tag libraries found in JSP, ColdFusion, etc. would be good for separating the 'coders' from the 'presentation designers' in large projects, but that's not pertinent to security.
What gets large PHP web applications like web based mail user agents, like Squirrelmail, and content management systems like PHPNuke is that they have to keep a lot of temporary resources between user actions in a single session. Resources such as open files, sockets, variables in memory, for instance.
PHP's simple engine, and its lack of language features to support these features forces PHP web application developers to build and destroy many of those resources in the life-cycle of the script instead of the life cycle of the application or the user session that is more appropriate.
The larger the application gets, the more difficult it gets to build using the simple 'web scripts' programming paradigm PHP and Perl pushes developers towards. This affects security and performance.
The end result is heavy dependence on databases for variable persistance and error-prone algorithm concoctions, a la PHPNuke et. al.
Based on upvotes, Ageism is the only "-ism" Slashdotters care about and think isn't SJW
I expected somebody to come in and say, "PHP's object support is not very good and OO and GOF patterns are the savior of code management and mega-reuse and getting dates, etc. etc." But I have not seen that. What's an OO critic to do? :-)
Table-ized A.I.
PHP 5 will have much much better XML support.
There is a lot of work on a complete new DOM extension, which should clean up the mess done in domxml as of PHP 4. It will follow the W3C DOM API as much as possible and is completely rewritten from scratch.
Furthermore Sterling Hughes is working on SimpleXml. An extension which should make it much easier to access XML Documents with the usual PHP functions.
The SAX Parser in PHP 5 will also be based on libxml2 and not anymore on the aging expat library.
XML Validation, XPath and XSLT support are also currently revised and should be improved a lot in PHP5.
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.
>>Recently I've had a chance to do some web design with PHP.
/anypattern/; } while();" as beyond their comprehension, but it's not the language's fault if they don't wanna use some the languages builtin features for their great advantage...
:)
some (?) web design (??)... how much of it to begin with?
>>Previously I'd used Perl because I'd heard from many people that Perl was the end all and be all of scripting languages for the web. Imagine my suprise to discover that PHP was vastly superior!
Perhaps your surprise came from the fact that you were using CGI Perl and that PHP was a fast Apache module? You'd be surprised to learn, though, that today there's also a Perl Apache module as well which is as fast or faster than PHP's...
>>I know this is a bold statement, but I have solid arguements to support it.
You better have them as we'll see below...
>>I'm not arguing that PHP is better than Perl in all cases. There is certainly still a use for Perl.
Actually, it's the contrary: Perl is one of the most flexible and faster high level languages in existence. It is used for nearly any tasks imaginable dealing mostly with character string processing, that is, it's an overall great general purpose Turing machine. PHP for some misterious reason is used solely as a tool for building html pages, or perhaps PDF using some commercial library...
Besides, you're aware that php was born from Perl, aren't you? They just dumbed it down so as not to scare ex-M$ users away and because of this the language just lost much of its strengh and flexibility. Ever heard of Perl's motto: "There's more than one way to do it"? It means you can express the same computation many different ways. It means, for instance, that you may express in purely procedural fashion or object oriented, where you see fit. In php (and Python as well) you're trapped in OO craze. There's just one way to do things.
Also, Perl is one of the most compact (and therefore faster) languages in existence. Of course, people's unwillingness to learn the language make them look at features such as "do { print if
So, i say, i'm not saying Perl is better than php in all cases, there's certainly still a use for php...
>>Finally, I'm not the most talented Perl programmer out there.
That seems obvious!
>>I generally prefer to use the vastly superior Python, but can use Perl if I have to.
I like Python too and highly regard it to be Perl's spiritual brother. But i wouldn't say one or the other is better. Though i would take Perl's flexibility and compactness over Python's rigid OO syntax anyday. One thing is true: they both beat the crap out of php and its VB-like stand...
>>* Ease of use. I would definitely not recommend anyone new to programming begin with Perl.
I wouldn't say ease of use is one of Perl's deficiencies. But surely the learning curve for the language is kinda steep. Still, power comes at a price and if you are willing to use a powerful tool you'd better learn how to use it and use it well.
I can say once you learn Perl right, most other languages just feel like lacking in flexibility, expressiveness or compactness.
Yes, if you never used a katana, you'd better get to grips first with a bamboo stick or something.
>> * The OO of PHP is excellent. In my experience, it rivals Smalltalk.
Who do you think you're fooling? PHP's OO features are more like Javascript than Smalltalk, stop daydreaming. If you want high level OO stuff go with Python's multi-inheritance model.
>>Hopefully Perl will be patched up so it supports such must-have OO features like introspection, reflection, self-replication and ontological data-points.
When there's real need for such features, yes. Because Perl doesn't force you into OO syntax, many constructs which just make sense in a purely OO view of the world are not really need
I don't feel like it...
Well, it'll be a module, not a built-in; Perl's built-ins are often powerful in the broad thermonuclear sense, but are never web-specific. Two key differences remain, though.
One: The Perl modules will have 15 dependencies each, 3 of which are no longer available at CPAN and which have to be hunted down from a Taiwanese mirror using archive.org.
Two: The various Perl modules you need will use different and incompatible pet data structures as preferred by their various developers, so you'll spend extra time writing glue code - vs PHP where you just use the function and go.
"Patriotism is your conviction that this country is superior to all other countries because you were born in it." -- GBS
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
Types aren't hidden in PHP
As anyone can plainly see
They are not hidden from your code,
They're right there from the time you load.
With gettype() they're revealed to you,
With *val() you can set them too.
There's so much flexibility
I can hardly contain my glee.
Setting, seeing, casting types,
PHP answers all your gripes.
"Patriotism is your conviction that this country is superior to all other countries because you were born in it." -- GBS