Php on the command line – part 1 article
PHP’s Command Line Interface first showed up in PHP 4.2.0, as experimental functionality. Install jdk With PHP 4.3.0 the CLI became an official SAPI (Server API). Install pip What that means in practical terms is that the CLI version of PHP now comes as a separate PHP binary (executable), which you can use to run scripts on the command line, as well as providing “access points” to the rest of the world, much like the $_GET and $_POST variables that give you access to incoming data over the Web.
Before I go any further I should mention that, as you’re probably aware, PHP is not the only choice for writing command line scripts. How to install php fusion Both Perl and Python, to name just two, are widely used for writing command line applications and, in many cases, make better a choice than PHP. Install drivers They provide a mature set of tools for common problems and, typically, better performance.
So, why use PHP? An obvious — and very good — reason may simply be that you know PHP better than the alternatives. How to install php for windows 7 Less obvious is that, if you’re developing a Web application in PHP, writing supporting scripts in another language can lead to extra headaches — even if you’re confident in both.
Php youtube There’s both the human aspect of having to switch programming “mind sets”, and the overhead of having to support two platforms and the potential missed opportunities for re-use; data access logic may need to be implemented twice, for example.
What’s more, if you’re building a PHP application that will be used by “unknown” third parties, requiring them to set up other platforms in addition to PHP is likely to cause frustration, particularly if they’re using Windows. Install facebook Although PHP may not be the best choice overall for writing command line utilities, it’s not a bad choice, either, and, in building tools to support an application that’s already written in PHP, it makes sense to stick to a single platform.
What types of problems are suited to being solved with a command line script? Obvious choices are anything that relates to the administration of the server on which your Web application runs, such as backups, purging and archiving old data, and analysing log files — perhaps sending an email alert should critical errors occur. Install zune software These are all tasks that are suited to automation with cron, the “intended user” of the scripts being another script or application.
It needn’t necessarily stop there, though. Install dropbox You might consider writing a command line “installer” for your Web application, to take care of copying PHP scripts to the right locations, setting up the correct file system permissions, loading the database schema and, for the intrepid, managing upgrades to the application. Free install windows xp service pack 2 When executing PHP scripts over the Web via Apache, you typically don’t have rights to complete tasks such as changing file system permissions a€“ plus, you don’t necessarily want to expose to the Web code that’s capable of making radical changes. Install eclipse However, run from the command line, using a normal user account, the script can be kept offline while providing you the necessary rights to copy files, change permissions and so on.
The advantage of command line scripts is that they’re usually easier to develop and, more importantly, quicker to use then Web-based of desktop GUIs. Java install error 1723 As such, they can also play a big part in your development process, helping you perform “build” tasks quickly and easily, such as generating API documentation with phpDocumentor or running your Simple Test suite. Install kodak printer Phing (a build tool based on Apache’s ANT) and rephlux (a continuous integration tool based on CruiseControl) provide frameworks to help you automate your development process. Install flash player free download Setting Up
I’ve already covered the basics of setting up PHP so you can run it from the command line in Getting Started with PEAR, Page 2. Php keyboard input It’s also worth being aware of what the manual has to say about Using PHP from the command line.
When writing your own command line scripts (as opposed to using the PEAR Package Manager), you need to be conscious of which PHP binary you’re using (your host, for example, may have set you up with the CGI binary).
Note the (cli) above — if instead you see “ (cgi)“, don’t worry. Install php download The CGI binary can be told to behave like the CLI binary when it’s used, but you need to be aware there’s a difference. Install safari I’ll look at solving compatibility issues between the two, as well as older PHP versions, at the end of this article. Php year Hello World
You can avoid specifically having to call the php binary first on UNIX-based systems by placing a SheBang right at the start of the script. Php qr code generator This “points” the shell at the binary that should be used to execute the script. Install opera Here’s an example:
On UNIX-based systems, you may also consider dropping the .php file extension, as it’s not required in order to execute the script, and because there is some benefit to be had by doing so; if, later, you choose to replace it with a script that’s written in another language, while maintaining the functionality, there’ll be no confusion.
On Windows, you can achieve almost the same thing by associating the PHP file extension with the PHP binary. Install hot water heater You’ll find this explained at the end of Replacing Perl Scripts with PHP Scripts.
Here, I’ll stick to using a .php extension, and I’ll assume you’ll be executing the scripts by first calling the PHP binary, as this makes the most effective cross-platform approach. Java install error 1603 Stream In, Stream Out
Instead of seeing the numbers displayed at the moment they are printed, with one-second intervals between them, they’re all flushed out at once when the script terminates (note that running the same script on Linux results in the numbers being displayed as they happen).
The other reason is more a question of good practice. How to install php windows PHP (the underlying shell, in fact) allows you to direct normal output that’s meant for the user to what’s known as the “standard out”, while diverting any error messages to the “standard error”. Install explorer 10 The advantage in making this division is that it allows you to log errors separately from messages generated by the normal, smooth operation of the script, which can be very useful when running batch jobs.
Part of PHP’s CLI interface are three “streams” with which you can interact in more or less the same way as you would a file resource returned from fopen(). Php install pear The streams are identified with the strings:
With the PHP 4.3.0+ CLI binary, these three streams are automatically available, identified with the constants STDIN, STDOUT and STDERR respectively. Php validation Here’s how I can use the STDOUT to fix the above script so that it behaves correctly on Windows:
What happens when the script is executed? Well, when the interpreter reaches the fgets() command (it sees the code reading from STDIN), execution pauses and waits for the user to hit enter. How to install phpmyadmin ubuntu server On pressing return, fgets() receives everything the user typed from the point at which execution paused, until the moment he or she pressed return. Install linux This includes the return linefeed character — see what happens if you display the contents of the $name variable after passing it through nl2br().
This “pause / continue” behaviour is actually controlled by the terminal (the command prompt). Install php server It works on a line-by-line basis and is known as Canonical Mode Input Processing.
Note that, on UNIX only, it’s possible to have your script process input in Non-canonical Mode using the pcntl_signal(), which allows you to respond directly to particular key sequences.
Note also that I’ve started to use the exit() command at the end of the script, passing it an integer value. Install kitchen faucet This is another question of “good manners”. Install kodi on chromecast Common practice on UNIX-based systems is for a script to return a status code when execution halts, zero being the standard for denoting the “All OK”. Php yahoo Other codes (integers between 1 and 254) are used to identify problems that caused the script to terminate prematurely (it’s up to you to define the error codes for you script, which is commonly achieved by defining a set of constants at the start). How to install php with apache If another command line script (perhaps written in another language) is used to execute your script, it will rely on the status code returned from exit() to determine whether you script ran successfully or not. How to install phpunit For some more thoughts on the subject, see the Advanced Bash-Scripting Guide on Exit and Exit Status.