Tips and Tricks: PHP Shell Scripts

by Mike Willbanks on April 24th, 2007


One of the many features of PHP that is over looked is utilizing PHP as a shell language (just like you can use perl). Many times I find that developers will create cron jobs that utilize wget or lynx to fetch the script and process it. This is not only a bad idea as the script resides in a publicly accessible location but it is more so a bad idea as it doesn’t utilize some of the great features things that PHP will automatically do for you in your configuration files. These things are automatically done by the PHP CLI, if not enabled you can configure PHP with the –enable-cli flag.


There are many features that you can utilize by using this as a shell script as I am outlining below. Some of these are simply php.ini changes that will happen by default and others are simply ways to retrieve information. Essentially what you can do using this tool is unlimited. There is even an Ncurses extension with documentation.

Default Configuration Changes

html_errors is changed to false. So you do not see html error messages on the command line and instead get nicely printed regular error messages (this does not mean if you put in html for error messages that it will not show up)!

implicit_flush is changed to true. Since we are on the command line, chances are that you do not want to buffer the content. If you have a need to buffer the content, this is still possible utilizing output buffering.

max_execution_time is changed to an unlimited time frame. Since you are likely not using this for a web site the data doesn’t need to time out for quick serving as it is most likely some form of utility script that was written.

register_argc_argv is changed to true. Typically one of the frequently asked questions is how to get input when calling a script from the command line. Well argc and argv will give you the solution you need. $argc (integer) will hold the number of arguments that were passed in and $argv (array) will hold the values. The keys of $argv are numeric.

Creating an Executable Script

To create an executable script the permissions on the script must have +x for executable and also have the path of php in the interpreter line (1st line of the file). For example:
Permissions: chmod +x script_name.php.
Interpreter Line: #!/path/to/php.
Executing: ./script_name in the current directory.


In the PHP CLI, STDIN, STDOUT and STDERR are already initialized, therefore it is rather simple to fetch and write data mindlessly. Each of these are defined by constants that can be utilized with the stream functions (fwrite, fgets, fseek, fscanf, etc). Here are a few examples:

Retrieve and Output Information:

< ?php
fwrite(STDOUT,  "Please enter in some input: ");
$line = trim(fgets(STDIN));
fwrite(STDOUT, $line . "\r\n");

Retrieve Information, Check for Errors and Output:

< ?php
fwrite(STDOUT,  "Please enter in an alphanumeric string: ");
$line = trim(fgets(STDIN));
if (!ctype_alpha($line)) {
    fwrite(STDERR, "The information you entered contained more information that alphanumeric characters.\r\n");
} else {
    fwrite(STDOUT, $line . "\r\n");

Arguments using argc and argv

Sometimes, you might have the need to create a shell script that will allow you to pass in the values at runtime. This simply happens using $argv and $argc. The count is contained in $argc. Note that there will always be 1 for the name of the script executed. Each argument is delimited by a space unless escaped or enclosed in quotes. So here is a simple example showing the input in a very simplistic way:

Retrieve Count and Print Out Variables

< ?php
if ($argc > 0) {
    fwrite(STDOUT, '# of arguments: ' . $argc . "\r\n");
    foreach ($argv as $k => $v) {
        fwrite(STDOUT, $k+1 . ': ' . $v . "\r\n");

Additional Resources

Nothing that was covered here was outside of the scope of the PHP manual. Look at the command line features in the PHP manual for items that I did not cover or for additional examples. In short, it is really easy to create PHP shell scripts that have many benefits and can be a great tool when used in the right context. Remember, use the best tool for the job. If you can do it, it doesn’t mean that you should.

From PHP

  1. Why would someone use PHP as a scripting language? That’d be a nice article.

  2. oh god… PHP as a scripting language! NOOOOOO!!!!!

  3. Andreas Kompanez permalink

    Taken from
    “What is PHP?

    PHP is a widely-used general-purpose SCRIPTING LANGUAGE that is especially suited for Web development and can be embedded into HTML. If you are new to PHP and want to get some idea of how it works, try the introductory tutorial. After that, check out the online manual, and the example archive sites and some of the other resources available in the links section. “

  4. Andreas Kompanez permalink

    Forgot to say, thanks Mike for that article. I’m using PHP for shell scripting all the time, but some of those php.ini directives were new for me.

  5. Nadeem permalink

    when i m writting

    fwrite(STDOUT,’Enter Name’);

    this kind of error is coming can anyone guide what the hell it is….

    PHP Notice: Use of undefined constant STDOUT – assumed ‘STDOUT’ in

  6. Nadeem,

    This might depend on your PHP version. Do you know what version you are running?
    Simple way to do this is to do one of the following:

    1. if you have shell access type in php -v
    2. echo out phpversion()

    Also are you executing this from within a shell environment or are you doing it straight out of your web browser because the web brower will not work with STDOUT.

  7. I would suggest to use “#!/usr/bin/env php” instead of a hard-coded path to php, as this allows for the php binary to be anywhere in the user’s PATH.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS