Reading commandline input in PHP

In PHP, all commandline parameter are passed to the $argv array. Following example shows how to use this array:

<?php
    $op1 = $argv[1];
    $op2 = $argv[2];
    $sum = $op1 + $op2;
    print 'Sum: ' . $sum . "\n";
    print_r($argv);
?>

$argv[1] and $argv[2] contain the passed parameters in the order they were typed. The last line prints a dump of the data where the first parameter is the name of the file.

$ php sum.php 2 3
Sum: 5
Array
(
    [0] => sum.php
    [1] => 2
    [2] => 3
)

Another way to read command line parameters is to use STDIN.

<?php
// request  input
fwrite(STDOUT, "Enter password: ");

// get input
$pass = trim(fgets(STDIN));

// write response
if ($pass === 'pass') {
    fwrite(STDOUT, "Welcome!");
} else {
    fwrite(STDOUT, "Wrong password");
}
print "\n";
?>

STDIN reads input from commandline and STDOUT writes something to the screen.

$ php pass.php 
Enter password: pass
Welcome!


buy cialis cheap