© Robert Aley 2016

Rob Aley, PHP CLI, 10.1007/978-1-4842-2238-6_1

1. Introduction

Rob Aley

(1)Oxford, UK

“Use PHP? We’re Not Building a Website, You Know!”

Both its current recursive moniker (PHP: PHP HyperText Preprocessor) and 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 afterward. In the web arena, PHP excels. It’s easy to use, quick to develop in, widely deployed, and tightly integrated into web stacks (it’s usually the P in LAMP, WAMP, MAMP, and so on), and of course it is free and open source.

But many people don’t realize (or haven’t noticed or choose not to notice) that PHP has evolved. It now closely resembles a modern, general-purpose programming language. This lack of recognition is partly PHP’s own fault because it took a long time to get some of the fundamentals in place, such as object-oriented programming (OOP) language constructs and even the ability to run at all without a web server being involved. Further, the programming community hasn’t 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 (for example, closures, traits, better language support, namespaces, and 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 leaner popular scripting languages. For even higher-performance needs, PHP 7 has dramatically rewritten the Zend Engine (giving up to twice the performance of 5.6), and Facebook (the biggest user of PHP around) is one of several companies releasing alternative interpreters/virtual machines (VMs), leading to performance increases of up to six 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 PHP’s 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 PHP’s integrated database access, reuse existing code and data from your web projects, take advantage of PHP’s 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?

Ideally I’ve sold you on the story that PHP is a cross-platform, rapid-development-focused, versatile language and is ideal for many different types of software. The aim of this book is to give you, the PHP coder, an insight into one specific type of software: using PHP to build command-line tools that live beyond the Web. I’ll show you the essential ingredients needed to transform your existing web-based PHP skills to the command line so you can get up and running as quickly as possible with the tools you already know.

Further Reading

Are You New to PHP?

This book shows PHP developers how they can use their existing skills to write CLI software instead of web pages. However, I appreciate that some readers may be new to PHP and are reading this book to get a feel for what PHP is capable of. If you’re already a programmer, albeit not well versed in PHP, the comprehensive official PHP manual may be the best place to begin to get a feel for the differences between PHP and the languages you are used to using.

If you’re not already a programmer, there are numerous “beginning PHP” books available from your favorite e-book retailer. In either case, the wider Web also provides its usual breadth of in-depth knowledge and tutorials, just a judicious Google search away.

Finally, newcomer or experienced programmer, if you’re thinking of getting serious about PHP, it may be worth taking a look at some PHP “best practice” websites before diving headlong into coding. It may save you a lot of trouble in the long run.

Further Reading

Reader Prerequisites

To make the most of this book, you should have basic experience of programming in PHP (most likely for the Web), a general programming or IT background, and a willingness to learn and be taken outside of your comfort zone.

This book isn’t an introduction to PHP or programming in general. Although you don’t need a computing degree or knowledge of advanced programming concepts , the book is aimed at the level of an average PHP programmer (one who has explored more than the basics of PHP) and tries to explain any necessary concepts as we go along. It is also useful for advanced programmers who may choose to use it as a quick reference for exploring the PHP CLI features.

An Important Note About Operating Systems

PHP runs on many operating systems , including Linux, Microsoft Windows, and Apple OS X, and code often runs in an identical manner. However, there are, of course, differences because of the file system, operating system (OS), available libraries, and so on, but covering these all in this book would not be practical. In addition, some features such as those reliant on POSIX standards aren’t (easily) available on some systems like Windows. As OS X was derived from a POSIX-compliant operating system , you will likely find more of the code compatible than with Windows, but your experience may still vary. A good source of information for OS-specific issues is the official PHP online manual and, in particular, the user comments at the bottom of each page. Where possible, areas specific to a particular OS will be covered; for instance, we will look at how to access the Windows Registry.

About the Sample Code

As you’ll see throughout the book I mainly use “traditional” imperative/procedural PHP in my coding examples to keep things as simple as possible for coders of all abilities. This book isn’t designed to be a lesson in coding best practices or style, a guide to OOP programming, or an endorsement of any kind of programming model or dogma. I also avoid the use of any code frameworks. Many frameworks are based around the web model and don’t always perform as intended in the kind of applications I’ll be covering, although some do now have “console” or “cli” modules. MVC-style frameworks can be useful when building graphical user interface (GUI) applications (indeed, the MVC paradigm predates the web considerably!), but because of the many different implementation details and styles, I’ll stick to framework-less code here.

It should be clear from the plain, straightforward PHP code presented how it can be used or adapted to suit your own programming style, framework, or model. I will be mainly talking about the task-specific implementation details, leaving the hot topic of programming itself to the many other books available.

All the sample PHP source code in this book is available for you to use and do with as you please without limit or license. Use or abuse it as you see fit!

If you have trouble running or understanding the sample code, see the “Giving Feedback and Getting Help and Support” section in Chapter 6 of this book for details on how to contact me for help.

External Resources

Throughout the book I will point you in the direction of external tools , resources, and information via toolboxes and “Further Reading” lists as follows:

Toolbox

A Toolbox Like This

Toolboxes like this contain details of useful online tools and installable software.

Main documentation and installation information

http://www.a-useful.tool/

Main documentation and installation information

http://wiki.a-useful.tool

Further Reading

  • Useful articles, tutorials, and reference information will be presented in “Further Reading” sections like this

    • https://www.very-useful-info.book/

PHP 7

The next generation of PHP, PHP 7 (don’t ask what happened to 6; it’s a long story), was recently released. All the code and the techniques I talk about in this book should run and operate in the same way whether you are using PHP 5 or PHP 7. When I talk about, for example, installing PHP, I’ll refer to PHP 5 (for example, sudo apt-get install php5-cli) because that is what most people are still using at the moment. However, if your platform supports 7, then go ahead and use that instead (for example, on the latest Ubuntu version at the time of writing, 16.04, the package php7.0-cli is now in the standard repositories); it should all work in the same way.

If you are lucky enough to have the choice of which version to use and are planning on writing CLI scripts , here are some points to consider:

  • Speed: The number-one thing you’ll notice with 7 is the increase in speed. While successive releases of 5.x versions have consistently pushed speed higher (and resource usage lower), version 7 takes quite a leap, even outperforming Facebook’s HHVM PHP in some benchmarks. While this is of course welcome on a web server, it is really noticeable on longer-running programs like typical command-line interface (CLI) scripts .

  • Security: There have been a few minor security enhancements in version 7, such as the filtering option added to the unserialize() function. Overall, the security picture looks broadly the same in terms of the design and implementation of the language, though. One security-related concern to bear in mind is the availability of security patches going forward. Currently patches are available for the 7.0, 5.6, and 5.5 versions. As newer versions are released, though (7.1 is in Alpha stage at the time of writing), the older versions will cease to receive security updates, so if you are sticking with 5.x for now, you may want to have a migration plan in mind (assuming you want to continue to use a supported version) for the inevitable upgrade.

  • Features and compatibility: Version 7 introduced new features and depreciated older ones (you can find full details on the PHP website). None of the CLI-specific features changed, but of course most of the new features are available to use within CLI scripts. The current adoption rate of version 7 appears to be much higher than that of version 5 when it first came out. However, the version 5 series will still be the most commonly installed for some time, so you may want to consider the needs of your audience before using version 7–specific constructs.

If you are careful, both versions can be installed alongside each other. See Appendix A for full details of compiling and installing PHP on various platforms.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset