This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
Welcome
About the author
Ive been programming in PHP since late 2000. Initially it wasnt by choice as my preferred languages at the time were Perl and Delphi (also known as Object Pascal). Things began to change after I graduated from the University of Leeds with a degree in Computer Science in 1999 and started out in a career as a freelance web developer. After only a couple of months I was offered the opportunity to take over a (relatively speaking) substantial government website contract from a friend who was exiting the freelance world for the safer and saner world of full-time employment. The only catch was that several thousand lines of code had already been written, and they were written in a relatively new language called PHP. Oh, and the only other catch was that I had about a week to learn it before taking over the site. So, as was the way at the time, I popped down to the local Waterstones bookshop (for the younger among you thats where we used to get books. Books made of paper. And we had to go out and get them. Or order online and wait days for them to be delivered. Did I mention they were made of paper?). With my paper copy of The Generic Beginners Complete Guide to PHP and MySQL for Dummies Compendium book in hand (I may not have recalled the title completely correctly) I settled down with a pint of ale (Im in Yorkshire at this point, remember) and set about reading it. A few days later I was coding like a pro (well, stuff was working) and 12 years later I havent looked back. After a varied career as a freelancer and starting up a couple of, er, start-ups (IT related and not) with varying (usually dismal) success, Im now a full-time programmer at The University of Oxford. My day job involves medium-scale data acquisition and management, statistical analysis and providing user interfaces for researchers and the public. The majority of my development work is done in PHP, either developing new projects or gluing together other peoples software, systems and databases.
Through-out my career Ive always used PHP for web development, but for Desktop GUI work I initially used Delphi (and then Free-Pascal/Lazarus), complimented with bash shell scripting for other tasks. This was mainly due to learning them while at University. However, as PHP has matured Ive increasingly used it beyond the web, and now I rarely use anything else for any programming or scripting task I encounter. Having been immersed in other languages like C++, Javascript, Fortran, Lisp (and probably others that my brain has chosen deliberately not to remember) by necessity during University and in some of my freelance jobs, I can honestly say that PHP is now my language of choice, rather than of necessity.
When Im not tied to a computer, I would like to say I have lots of varied and interesting hobbies. I used to have. I could write a whole book (which wouldnt sell well) about where Ive been and what Ive done and Id like to think its made me a well rounded person. But these days I dont. In large part this is due to the demands of my three gorgeous young daughters Ellie, Izzy and Indy, my gorgeous wife Parv and my even more gorgeous cat Mia. And I wouldnt have it any other way. Thats what I tell myself, anyway
Acknowledgements
Isaac Newton said If I have seen further, it is by standing on the shoulders of giants. This book builds on, and hopefully adds to, the work of many others. The most notable of whom I would like to acknowledge below.
The authors of, and contributors to, the official PHP manual
An invaluable reference for PHP functions and syntax, to which I referred frequently during writing this book, both for fact checking and as an aide-memoir. Thanks!
The collective PHP wisdom of the Internet
For over 12 years Ive used you for learning, research, play and profit. Too many sites & too many people to list here, if youve written about PHP on web then you may well be one of them. Thanks!
My Family
For allowing me a modicum of time to write this book, and supporting me unconditionally in everything I do. Usually. If I ask first. And theres not something more important going on. And usually with conditions. Thanks!
Introduction
If your e-reader has bought you directly to this page, or your fingers have flipped straight through the pages of the paper copy to here, then you have (perhaps wisely) skipped the books front matter, containing the front cover, table of contents, about-the-author section, acknowledgements and a few more bits and bobs. If that interests you, then scroll or flip backwards. Otherwise, read on to start your journey through the wider world of PHP software.
1.1 Use PHP? Were not building a website, you know!.
Both its current recursive moniker (PHP : PHP HyperText Preprocessor), as well as the name originally bestowed upon it by its creator Rasmus Lerdorf (PHP : Personal Home Page) reinforce the widely held view that PHP is a scripting language for the web. And that was true, back in 1995 when PHP was first created, and for a number of years afterwards. In the web arena PHP excels : its easy to use, quick to develop in, widely deployed, tightly integrated into web stacks (its usually the P in LAMP,WAMP,MAMP etc.), and of course because it is free and open source.
But many people dont realise (or havent noticed, or choose not to notice) that PHP has evolved and grown up. It now closely resembles a modern, general purpose programming language. This lack of recognition is partly PHPs own fault as it took a long time to get some of the fundamentals in place, such as OOP language constructs and even the ability to run at all without a web server being involved. However the programming community hasnt helped; many programmers had a hard time seeing the potential for PHP to bring its rapid dynamic development model out of the web and into the wider computing environment, and many simply stuck with the web scripting for beginners dogma that was only really true of its early years.
Recent releases in particular have brought mainstream language features (e.g. closures, traits, better language support, namespaces & late static binding among many others) to the table. Performance has jumped up and up and up, memory usage (a bugbear of older versions) has dropped considerably, and PHP is now one of the more lean popular scripting languages. For even higher performance needs PHP 7 has dramatically rewritten Zend Engine (giving up to twice the performance of 5.6), and Facebook (the biggest user of PHP around) are one of several groups who are releasing alternative interpreters/VMs, leading to performance increases of up to 6 times that achievable with the old Zend Engine. The recent release of PHP 7, and the upswing in community involvement, are indicative that reports of PHPs demise are quite premature!
But why, why, oh why create Command Line Scripts in PHP? Why not whip up a bash shell script? Why not learn C++ or another language typically used for software projects? The truth is these are valid options, and life may well work out just fine for you. But why turn down the opportunity to use your existing skills? Why not use PHPs integrated database access, reuse existing code and data from your web projects, take advantage of PHPs easy to use network libraries and functions, wallow in flexible text and data handling, and mix shell commands and other languages into PHP as needed to get best of both worlds? In short, why the heck not?