ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

PHP on the command line

(Page 2 out of 2)

Passing arguments

When using PHP CLI it's also possible to pass arguments to scripts, and using those arguments in the script. The below script demonstrates this:

#!/usr/bin/php -q

        echo "The following arguments were passed:\n";
        print_r ($_SERVER['argv']);
?>

To call a script with arguments type the arguments after the script name, like so:

C:\PHP\CLI\php.exe -q arguments.php one two three

The result will be:

The following arguments were passed:
Array
(
    [0] => arguments.php
    [1] => one
    [2] => two
    [3] => three
)

Arguments can be used for various things, like passing extra values or doing different tasks. For example, the following script is a simple PHP CLI calculator to demonstrate the use of arguments:

#!/usr/bin/php -q

        echo "\n\nPHP CLI Calculator:\n\n";

        $args = $_SERVER['argv'];

        if (count($args) != 4) {
                echo 'Please pass two numbers and an action';
        }

        $num1 = intval($args['2']);
        $num2 = intval($args['3']);

        echo 'Answer: ';

        switch(strtolower($args['1'])) {
                case 'add':
                        echo $num1 . ' + ' . $num2 . ' = ' . ($num1+$num2);
                        break;
                case 'subtract':
                        echo $num1 . ' - ' . $num2 . ' = ' . ($num1-$num2);
                        break;
                case 'multiply':
                        echo $num1 . ' * ' . $num2 . ' = ' . ($num1*$num2);
                        break;
                case 'divide':
                        echo $num1 . ' / ' . $num2 . ' = ' . ($num1/$num2);
                        break;
        }
?>

To use the above script, you have to call the script with three arguments: the action and two numbers, e.g.

C:\PHP\CLI\php.exe -q calculator.php divide 3 4
C:\PHP\CLI\php.exe -q calculator.php add 1 7
C:\PHP\CLI\php.exe -q calculator.php subtract 10 3
C:\PHP\CLI\php.exe -q calculator.php multiply 6 2

Let's have a look at another unique command line feature: waiting for input from the user.

Getting input

With the command line it's also possible to ask for user input by reading from STDIN. This special constant can be used to get input through the command line, and then handle that input in your script. The below example demonstrates this:

#!/usr/bin/php -q

        echo "\n\nWhat is your name?\n";
        $name = fread(STDIN, 1024);
        echo "\nWelcome $name";
?>

In the above example your name will be asked, and then printed back out. This can be extremely useful to pass values, instead of using arguments. Consider our calculator example above, and the below revised version:

#!/usr/bin/php -q

        echo "\n\nPHP CLI Calculator:\n\n";

        echo "What would you like to do? (add|subtract|multiply|divide)\n";
        $action = strtolower(fread(STDIN, 1024));
        $action = trim($action);

        $valid = array('add', 'subtract', 'multiply', 'divide');
        if (in_array($action, $valid) == false) {
                echo "\nInvalid action";
                die();
        }

        echo "\nEnter a number: ";
        $num1 = trim(fread(STDIN, 1024));

        if (is_numeric($num1) == false) {
                echo "\nInvalid number.";
                die();
        }

        echo "Enter a second number: ";
        $num2 = trim(fread(STDIN, 1024));

        if (is_numeric($num2) == false) {
                echo "\nInvalid number.";
                die();
        }

        echo "\nAnswer: ";

        switch(strtolower($action)) {
                case 'add':
                        echo $num1 . ' + ' . $num2 . ' = ' . ($num1+$num2);
                        break;
                case 'subtract':
                        echo $num1 . ' - ' . $num2 . ' = ' . ($num1-$num2);
                        break;
                case 'multiply':
                        echo $num1 . ' * ' . $num2 . ' = ' . ($num1*$num2);
                        break;
                case 'divide':
                        echo $num1 . ' / ' . $num2 . ' = ' . ($num1/$num2);
                        break;
        }
?>

If you now run the above script, it won't need any arguments, but instead it will ask you for the action and numbers. It does exactly the same as the previous script, but it's much easier to enter the values.

Conclusion

In this tutorial I've shown you how to run PHP scripts from the command line, and showed you a few specific command line features, like STDIN.

Although the examples in this tutorial were very simply, and had very little use, the PHP CLI can actually be extremely useful, and almost anything can be done, especially because PHP scripts that are run on the command line have full system capabilities which means it can write to almost any directory.

Good candidates for command line scripts are backup scripts, and other tasks that need to run hourly/daily/weekly. If you combine PHP CLI with Windows Task Scheduler or Cron jobs you can do certain tasks automatically, like clearing data caches, retrieving RSS feeds, or even crawling other websites.

If you have any questions or comments on this tutorial, leave them below or join us at PHPit Forums for more PHP talk.

« Previous: The Basics



3 Responses to “PHP on the command line”

  1. Richard@Home » Blog Archive » links for 2006-03-14 Says:

    […] PHPit - Totally PHP » PHP on the command line A tutorial covering how to run php scripts from the command line. (tags: php cmd) […]

  2. PHPit - Totally PHP » An Introduction to the Winbinder Library, Part 1 Says:

    […] PHP is used for many different things, and almost anything is possible, including command line scripts (see a recent article on PHPit on command line script). But it’s also possible to create full-blown Windows applications using PHP and the Winbinder library, and that’s what we’ll be looking at in this new article series. […]

  3. Dion Says:

    If you want an alternative for Unix/Linux’s #! syntax, take a look here:
    http://whitescreen.nicolaas.net/php_polyglot.php

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. The Basics
  2. Arguments & STDIN
Bookmark Article
Download Article
PDF
Download this article as a PDF file