Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
knowledge_base:programming:php_cli [2022/12/02 14:55] – removed - external edit (Unknown date) 127.0.0.1 | knowledge_base:programming:php_cli [2022/12/02 14:55] (current) – ↷ Page moved from knowledge_base:php_cli to knowledge_base:programming:php_cli George Wayne | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== PHP Basics ====== | ||
+ | ===== Hello World ===== | ||
+ | |||
+ | In its most basic form, the CLI interface can be used just like any PHP script. Here’s an example: | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | print "Hello World!"; | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: helloworld.php | ||
+ | |||
+ | You can execute this script by typing: | ||
+ | |||
+ | php helloworld.php | ||
+ | |||
+ | 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. This “points” the shell at the binary that should be used to execute the script. Here’s an example: | ||
+ | |||
+ | < | ||
+ | # | ||
+ | <?php | ||
+ | print "Hello World!"; | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Note the path to your PHP executable may vary, ''/ | ||
+ | |||
+ | I can now execute this script with: | ||
+ | |||
+ | < | ||
+ | |||
+ | Oh wait... If you’re running on UNIX based system, you first need to make this script executable with: | ||
+ | |||
+ | < | ||
+ | |||
+ | |||
+ | 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, | ||
+ | |||
+ | On Windows, you can achieve almost the same thing by associating the PHP file extension with the PHP binary. 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. | ||
+ | |||
+ | ===== Stream In, Stream Out ===== | ||
+ | |||
+ | When it comes to displaying output from your command line scripts, using the normal '' | ||
+ | |||
+ | First, on Windows, see what happens when you execute the following from a command prompt: | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | $i = 0; | ||
+ | while ( $i < 10 ) { | ||
+ | print $i." | ||
+ | sleep(1); | ||
+ | $i++; | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: printproblem.php | ||
+ | |||
+ | 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. 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”. 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 '' | ||
+ | |||
+ | < | ||
+ | php://stdin (read) | ||
+ | php:// | ||
+ | php:// | ||
+ | </ | ||
+ | |||
+ | With the PHP 4.3.0+ CLI binary, these three streams are automatically available, identified with the constants STDIN, STDOUT and STDERR respectively. Here’s how I can use the STDOUT to fix the above script so that it behaves correctly on Windows: | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | $i = 0; | ||
+ | while ( $i < 10 ) { | ||
+ | // Write some output | ||
+ | fwrite(STDOUT, | ||
+ | sleep(1); | ||
+ | $i++; | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: streamout.php | ||
+ | |||
+ | The count should now be displayed at the moment at which '' | ||
+ | |||
+ | Note that, on Windows, PHP takes care of linefeed characters for you, so that they display correctly (you weren’t trying to use ''< | ||
+ | |||
+ | To be able to read input from the script’s user, you can use STDIN combined with '' | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | fwrite(STDOUT, | ||
+ | |||
+ | // Read the input | ||
+ | $name = fgets(STDIN); | ||
+ | |||
+ | fwrite(STDOUT, | ||
+ | |||
+ | // Exit correctly | ||
+ | exit(0); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: nameplease.php | ||
+ | |||
+ | What happens when the script is executed? Well, when the interpreter reaches the '' | ||
+ | |||
+ | This “pause / continue” behaviour is actually controlled by the terminal (the command prompt). 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 '' | ||
+ | |||
+ | Note also that I’ve started to use the '' | ||
+ | |||
+ | |||
+ | --------- | ||
+ | |||
+ | ===== Multiple Choice ===== | ||
+ | |||
+ | You can also use STDIN to handle choices: | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | fwrite(STDOUT, | ||
+ | |||
+ | // An array of choice to color | ||
+ | $colors = array ( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ); | ||
+ | |||
+ | fwrite(STDOUT, | ||
+ | |||
+ | // Display the choices | ||
+ | foreach ( $colors as $choice => $color ) { | ||
+ | fwrite(STDOUT, | ||
+ | } | ||
+ | |||
+ | // Loop until they enter ' | ||
+ | do { | ||
+ | // A character from STDIN, ignoring whitespace characters | ||
+ | do { | ||
+ | $selection = fgetc(STDIN); | ||
+ | } while ( trim($selection) == '' | ||
+ | |||
+ | if ( array_key_exists($selection, | ||
+ | fwrite(STDOUT, | ||
+ | } | ||
+ | |||
+ | } while ( $selection != ' | ||
+ | |||
+ | exit(0); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: pickacolor.php | ||
+ | |||
+ | The end user sees something like this: | ||
+ | |||
+ | < | ||
+ | Pick some colors (enter the letter and press return) | ||
+ | Enter ' | ||
+ | a: Red | ||
+ | b: Green | ||
+ | c: Blue | ||
+ | b | ||
+ | You picked Green | ||
+ | c | ||
+ | You picked Blue | ||
+ | q | ||
+ | </ | ||
+ | |||
+ | Using a loop, I can get my script to continue execution until the user enters ‘q’ to quit. Another approach would be a while loop that continues forever, but contains a break instruction when some condition is met. It may take you some time to get familiar with this, particularly if you’ve been using PHP in a Web application in which execution normally halts automatically after 30 seconds, and user input is provided in a single bundle at the start of a request. The main thing is to pay close attention to your code when you insert “never ending” loops like this, so you’re not inadvertently executing a database query over and over, for example. | ||
+ | |||
+ | Now, let’s get back to the example above. Have a look at what’s happening here: | ||
+ | |||
+ | < | ||
+ | // A character from STDIN, ignoring whitespace characters | ||
+ | do { | ||
+ | $selection = fgetc(STDIN); | ||
+ | } while ( trim($selection) == '' | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | If that doesn’t make sense, try commenting out the '' | ||
+ | |||
+ | Also, right now, users of this script can enter multiple characters between presses of the return key. See if you can work out a way to prevent that by confirming that return was pressed between the user’s choices. | ||
+ | |||
+ | ===== Errors and Pipes ===== | ||
+ | |||
+ | Earlier on, I mentioned that you could write errors to a different output location defined by the STDERR constant in PHP. This is achieved in the same way as writing to STDOUT: | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | // A constant to be used as an error return status | ||
+ | define (' | ||
+ | |||
+ | // Try connecting to MySQL | ||
+ | if ( !@mysql_connect(' | ||
+ | // Write to STDERR | ||
+ | fwrite(STDERR, | ||
+ | exit(DB_CONNECTION_FAILED); | ||
+ | } | ||
+ | |||
+ | fwrite(STDOUT," | ||
+ | exit(0); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: failedconnection.php | ||
+ | |||
+ | Any connection errors will now be reported using STDERR. What this script doesn’t do is demonstrate why this functionality can be useful. For that, consider the following: | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | // A custom error handler | ||
+ | function CliErrorHandler($errno, | ||
+ | fwrite(STDERR," | ||
+ | } | ||
+ | // Tell PHP to use the error handler | ||
+ | set_error_handler(' | ||
+ | |||
+ | fwrite(STDOUT," | ||
+ | |||
+ | // File does not exist - error is generated | ||
+ | if ( $fp = fopen(' | ||
+ | // do something with the file here | ||
+ | fclose($fp); | ||
+ | } | ||
+ | |||
+ | fwrite(STDOUT," | ||
+ | exit(0); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: outwitherrors.php | ||
+ | |||
+ | Now, when you execute this script normally (assuming the file foobar.log doesn’t exist in the same directory as the script), it produces output like this: | ||
+ | |||
+ | Opening file foobar.log | ||
+ | fopen(foobar.log): | ||
+ | Job finished | ||
+ | |||
+ | The error messages are mixed with the normal output as before. But by piping the output from the script, I can split the errors from the normal output: | ||
+ | |||
+ | < | ||
+ | |||
+ | This time, you’ll only see these messages: | ||
+ | |||
+ | < | ||
+ | Opening file foobar.log | ||
+ | Job finished | ||
+ | </ | ||
+ | |||
+ | But, if you look into the directory in which you ran the script, a new file called errors.log will have been created, containing the error message. The number 2 is the command line handle used to identify STDERR. Note that 1 is handle for STDOUT, while 0 is the handle for STDERR. Using the ''>'' | ||
+ | |||
+ | Although this may not seem very exciting, it’s a very handy tool for system administration. A simple application, | ||
+ | |||
+ | < | ||
+ | $ php outwitherrors.php >> transaction.log 2>> errors.log | ||
+ | </ | ||
+ | |||
+ | Using ‘''>>'' | ||
+ | |||
+ | There’s one difference between the UNIX and Windows command lines, when it comes to piping output, of which you should be aware. On UNIX, you can merge the STDOUT and STDERR streams to a single destination, | ||
+ | |||
+ | < | ||
+ | $ php outwitherrors.php > everything.log 2>&1 | ||
+ | </ | ||
+ | |||
+ | What this does is re-route the STDERR to STDOUT, meaning that both get written to the log file everything.log. | ||
+ | |||
+ | Unfortunately, | ||
+ | |||
+ | In general, the subject of piping IO is one that I can’t do full justice to. On UNIX, it’s almost a (black?) art, and usually comes into play when you start using other command line tools like sed, awk and xargs (O’Reilly have dedicated entire books to the subject, including the Loris Book). What you should be getting a feeling for, however, is that, by conforming to the standard conventions for shell scripting, you have the chance to tap into a powerful system administration “framework” that provides all sorts of other tools. | ||
+ | |||
+ | ===== Coping with Arguments ===== | ||
+ | |||
+ | You’ve already seen some examples of how to read user input from STDIN, once a script has already begun execution. What about being able to pass information to the script at the point it is executed? | ||
+ | |||
+ | If you’ve ever programmed in a language like C or Java, you’ll no doubt be familiar with variables called “argc” and “argv”. With PHP, the same naming convention is used, with the integer variable '' | ||
+ | |||
+ | To see how they work, try the following script: | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | // Correct English grammar... | ||
+ | $argc > 1 ? $plural = ' | ||
+ | |||
+ | // Display the number of arguments received | ||
+ | fwrite(STDOUT," | ||
+ | |||
+ | // Write out the contents of the $argv array | ||
+ | foreach ( $argv as $key => $value ) { | ||
+ | fwrite(STDOUT," | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: arguments.php | ||
+ | |||
+ | I execute this script as normal: | ||
+ | |||
+ | < | ||
+ | |||
+ | The result is: | ||
+ | |||
+ | < | ||
+ | Got 1 argument | ||
+ | 0 => arguments.php | ||
+ | </ | ||
+ | |||
+ | The first and only argument is the name of the script being executed. | ||
+ | |||
+ | Now, if I execute the following: | ||
+ | |||
+ | < | ||
+ | $ php arguments.php Hello World! | ||
+ | </ | ||
+ | |||
+ | The code returns: | ||
+ | |||
+ | < | ||
+ | Got 3 arguments | ||
+ | 0 => arguments.php | ||
+ | 1 => Hello | ||
+ | 2 => World! | ||
+ | </ | ||
+ | |||
+ | The strings “Hello” and “World!” are treated as two separate arguments. | ||
+ | |||
+ | If, instead, I use quotes: | ||
+ | |||
+ | < | ||
+ | $ php arguments.php "Hello World!" | ||
+ | </ | ||
+ | |||
+ | The output is as follows: | ||
+ | |||
+ | < | ||
+ | Got 2 arguments | ||
+ | 0 => arguments.php | ||
+ | 1 => Hello World! | ||
+ | </ | ||
+ | |||
+ | As you can see, passing basic arguments to a PHP script in this manner is very easy. Of course, there are always a few “gotchas” to watch out for. | ||
+ | |||
+ | The variables '' | ||
+ | |||
+ | The other main “gotcha” is that first argument you saw above — the name of the script itself. This happened because I named, in the command line, the PHP binary to execute the script, so arguments are calculated relative to the PHP binary, not to the script it’s executing. Meanwhile, as you saw above, it’s possible to execute a script directly by identifying the binary with the “SheBang” on UNIX, or by file association on Windows. Doing so will mean the script’s name no longer appears in the list of arguments. In other words, you need to be careful to check what the first argument contains and whether it matches the contents of '' | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | if ( realpath($_SERVER[' | ||
+ | fwrite(STDOUT,' | ||
+ | } else { | ||
+ | fwrite(STDOUT,' | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: whereami.php | ||
+ | |||
+ | If you’ve had any experience executing commands using a UNIX shell, you’ll have no doubt run into the use of command line options such as: | ||
+ | |||
+ | < | ||
+ | $ ls -lt | ||
+ | </ | ||
+ | |||
+ | This lists the contents of a directory in “long” format, the files being ordered by modification time. Another example you may know is as follows: | ||
+ | |||
+ | < | ||
+ | $ mysql --user=harryf --password=secret | ||
+ | </ | ||
+ | |||
+ | The above is used to log in to MySQL. | ||
+ | |||
+ | These are known as command line options, the first example having the “short option” syntax, while the second uses the “long option” syntax. | ||
+ | |||
+ | If I try passing arguments like this to my arguments.php script above: | ||
+ | |||
+ | < | ||
+ | $ php arguments.php --user=harry --password=secret | ||
+ | </ | ||
+ | |||
+ | I get: | ||
+ | |||
+ | < | ||
+ | Got 3 arguments | ||
+ | 0 => arguments.php | ||
+ | 1 => --user=harry | ||
+ | 2 => --password=secret | ||
+ | </ | ||
+ | |||
+ | The $argv array is ignorant to the options syntax, restricting itself to breaking up arguments using the space character between them. It’s up to you to parse the contents of each argument to handle “options” like this within your script. | ||
+ | |||
+ | ------------- | ||
+ | |||
+ | ===== Parsing Options ===== | ||
+ | |||
+ | While, at first glance, it may not seem too hard to write a function for parsing options, why would you bother? PEAR:: | ||
+ | |||
+ | Note the version of Console_Getopt I used here was 1.2. See Getting Started with PEAR if you’re wondering how to install it. Be warned that uninstalling Console_Getopt is a bad idea, as the package manager itself depends on it. If you need to upgrade your version, use: | ||
+ | |||
+ | < | ||
+ | |||
+ | PEAR:: | ||
+ | |||
+ | 1. '' | ||
+ | |||
+ | The array returned from '' | ||
+ | |||
+ | 2. '' | ||
+ | |||
+ | The '' | ||
+ | |||
+ | < | ||
+ | |||
+ | The ‘short_opts‘ string identifies short options that the user can provide, followed by characters that identify given options. It allows two special markers in the string: | ||
+ | ‘'':'' | ||
+ | |||
+ | If '' | ||
+ | |||
+ | < | ||
+ | $ php some_script.php -lt | ||
+ | $ php some_script.php -tl | ||
+ | $ php some_script.php -l -t | ||
+ | $ php some_script.php -t -l | ||
+ | $ php some_script.php -l | ||
+ | $ php some_script.php -t -t -l -l | ||
+ | $ php some_script.php -t | ||
+ | $ php some_script.php | ||
+ | </ | ||
+ | |||
+ | Setting '' | ||
+ | |||
+ | < | ||
+ | |||
+ | Here ''' | ||
+ | |||
+ | < | ||
+ | |||
+ | Now ''' | ||
+ | |||
+ | < | ||
+ | |||
+ | Here ''' | ||
+ | |||
+ | Finally, setting '' | ||
+ | |||
+ | This may seem a little arcane, but specifying the short options string in this way is fairly standard in many languages used on UNIX, from C to Python. Once you get used to it, this method provides a useful mechanism to get the most out of command line arguments with minimum effort. | ||
+ | |||
+ | The '' | ||
+ | |||
+ | < | ||
+ | $long_opts = array ( | ||
+ | ' | ||
+ | ' | ||
+ | ); | ||
+ | </ | ||
+ | |||
+ | In this case, both the ''' | ||
+ | |||
+ | The array returned from '' | ||
+ | |||
+ | The final method provided by Console_Getopt is: | ||
+ | |||
+ | 3. '' | ||
+ | |||
+ | This method is almost exactly the same as '' | ||
+ | |||
+ | < | ||
+ | |||
+ | Here’s a simple example of PEAR:: | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | // Include PEAR:: | ||
+ | require_once ' | ||
+ | |||
+ | // Define exit codes for errors | ||
+ | define(' | ||
+ | define(' | ||
+ | |||
+ | // Reading the incoming arguments - same as $argv | ||
+ | $args = Console_Getopt:: | ||
+ | |||
+ | // Make sure we got them (for non CLI binaries) | ||
+ | if (PEAR:: | ||
+ | fwrite(STDERR, | ||
+ | exit(NO_ARGS); | ||
+ | } | ||
+ | |||
+ | // Short options | ||
+ | $short_opts = ' | ||
+ | |||
+ | // Long options | ||
+ | $long_opts = array( | ||
+ | ' | ||
+ | ' | ||
+ | ); | ||
+ | |||
+ | // Convert the arguments to options - check for the first argument | ||
+ | if ( realpath($_SERVER[' | ||
+ | $options = Console_Getopt:: | ||
+ | } else { | ||
+ | $options = Console_Getopt:: | ||
+ | } | ||
+ | |||
+ | // Check the options are valid | ||
+ | if (PEAR:: | ||
+ | fwrite(STDERR, | ||
+ | exit(INVALID_OPTION); | ||
+ | } | ||
+ | |||
+ | print_r($options); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | If we invoke this script like so: | ||
+ | |||
+ | < | ||
+ | $ php getopts.php -ltr --user=harryf --pass=secret arg1 arg2 | ||
+ | </ | ||
+ | |||
+ | The output from print_r() looks like this: | ||
+ | |||
+ | < | ||
+ | Array | ||
+ | ( | ||
+ | [0] => Array | ||
+ | ( | ||
+ | [0] => Array | ||
+ | ( | ||
+ | [0] => l | ||
+ | [1] => | ||
+ | ) | ||
+ | |||
+ | [1] => Array | ||
+ | ( | ||
+ | [0] => t | ||
+ | [1] => r | ||
+ | ) | ||
+ | |||
+ | [2] => Array | ||
+ | ( | ||
+ | [0] => --user | ||
+ | [1] => harryf | ||
+ | ) | ||
+ | |||
+ | [3] => Array | ||
+ | ( | ||
+ | [0] => --pass | ||
+ | [1] => secret | ||
+ | ) | ||
+ | |||
+ | ) | ||
+ | |||
+ | [1] => Array | ||
+ | ( | ||
+ | [0] => arg1 | ||
+ | [1] => arg2 | ||
+ | ) | ||
+ | |||
+ | ) | ||
+ | </ | ||
+ | |||
+ | The options array always contains two first order elements, the first of which contains another array that represents each command line option. The second element contains an array of arguments. If you look back at how the script was invoked, you should be able to see the relationship. | ||
+ | |||
+ | You can see how PEAR:: | ||
+ | |||
+ | < | ||
+ | |||
+ | If you parsed the above command line with Console_Getopt, | ||
+ | |||
+ | < | ||
+ | Array | ||
+ | ( | ||
+ | [0] => Array | ||
+ | ( | ||
+ | [0] => Array | ||
+ | ( | ||
+ | [0] => l | ||
+ | [1] => | ||
+ | ) | ||
+ | |||
+ | [1] => Array | ||
+ | ( | ||
+ | [0] => t | ||
+ | [1] => | ||
+ | ) | ||
+ | |||
+ | [2] => Array | ||
+ | ( | ||
+ | [0] => r | ||
+ | [1] => | ||
+ | ) | ||
+ | |||
+ | [3] => Array | ||
+ | ( | ||
+ | [0] => --width | ||
+ | [1] => 80 | ||
+ | ) | ||
+ | ) | ||
+ | |||
+ | [1] => Array | ||
+ | ( | ||
+ | [0] => / | ||
+ | ) | ||
+ | |||
+ | ) | ||
+ | </ | ||
+ | |||
+ | You’re now set to re-implement ‘ls’ in PHP, should you have endless hours on your hands. | ||
+ | |||
+ | ===== Compatibility ===== | ||
+ | |||
+ | As I mentioned earlier in this article, it’s possible to use the PHP CGI binary in more or less the same way as the CLI binary, but some additional “tweaking” is needed. Some of these changes are best made at runtime by including an additional PHP script. Others need to be made either in php.ini itself, or by passing additional arguments to the PHP binary. | ||
+ | |||
+ | Starting with the settings that you can change at runtime, here’s a script that fixes most of the issues for users working with the CGI binary, or CLI versions below 4.3.0: | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | /** | ||
+ | * Sets up CLI environment based on SAPI and PHP version | ||
+ | */ | ||
+ | if (version_compare(phpversion(), | ||
+ | // Handle output buffering | ||
+ | @ob_end_flush(); | ||
+ | ob_implicit_flush(TRUE); | ||
+ | |||
+ | // PHP ini settings | ||
+ | set_time_limit(0); | ||
+ | ini_set(' | ||
+ | ini_set(' | ||
+ | ini_set(' | ||
+ | |||
+ | // Define stream constants | ||
+ | define(' | ||
+ | define(' | ||
+ | define(' | ||
+ | |||
+ | // Close the streams on script termination | ||
+ | register_shutdown_function( | ||
+ | create_function('', | ||
+ | ' | ||
+ | ); | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: cli_compatibility.php | ||
+ | |||
+ | Including this code in your scripts will resolve most of the problems, but some further issues remain. | ||
+ | |||
+ | The CGI executable normally sends HTTP headers when executed — even from the command line. Users are likely to see output similar to the following when they execute command line scripts using the CGI executable: | ||
+ | |||
+ | < | ||
+ | X-Powered-By: | ||
+ | Content-type: | ||
+ | </ | ||
+ | |||
+ | To prevent this, PHP needs to be invoked with the ‘-q’ option for ‘quiet’. For example: | ||
+ | |||
+ | < | ||
+ | $ php -q some_script.php | ||
+ | </ | ||
+ | |||
+ | Unfortunately, | ||
+ | |||
+ | < | ||
+ | # | ||
+ | <?php | ||
+ | // Code starts here | ||
+ | </ | ||
+ | |||
+ | Another issue with the CGI executable is that it changes automatically the current working directory to that in which the script that’s being executed resides. Imagine I use a simple script that contains the following: | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | print getcwd()." | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Filename: getcwd.php | ||
+ | |||
+ | Now, I execute it like so: | ||
+ | |||
+ | < | ||
+ | $ pwd | ||
+ | / | ||
+ | $ php ./ | ||
+ | </ | ||
+ | |||
+ | If I’m using the CLI binary, this will display “/ | ||
+ | |||
+ | I’m not aware of a workaround that would solve this issue, so, if it’s critical for your application, | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | if ( php_sapi_name() == ' | ||
+ | die (' | ||
+ | } | ||
+ | if ( version_compare(phpversion(), | ||
+ | </ | ||
+ | |||
+ | ===== Wrap Up ===== | ||
+ | |||
+ | That finishes this first part of our tour of PHP’s command line interface. So far, we’ve got the basics covered. Next time, I’ll discuss the execution of external programs from PHP, we’ll explore some of the packages PEAR has to offer for sprucing up your output, and we’ll take a look at some of the (UNIX only) extensions PHP provides for the command line. | ||
+ | |||
+ | Read more at: http:// |