ryoakg
5/3/2016 - 11:02 PM

cli.php

<?php
if (PHP_SAPI !== 'cli') throw new Exception(PHP_SAPI . ' is not supported SAPI.');
// write your app below.

// It's not recommended to use `$_ENV`.
// Because it's not too portable.
echo '$_ENV: ';
var_dump($_ENV);

// Did you see any environment variables?
// If you want to use it, make sure to put letter 'E' in `variables-order` in your `php.ini`.
// cf. http://us.php.net/manual/en/ini.core.php#ini.variables-order
// You can run `php --ini` to get where your `php.ini` is.
// And also you can run `php -c PHP.INI SCRIPT_NAME.php` to specify `php.ini` temporarily.
// And also you may use `$_SERVER` instead.
// You'll may see environment variables hear.
echo '$_SERVER: ';
var_dump($_SERVER);

// But it might be not too portable too.
// However you can use `getnev()` instead.
echo '$LANG: ' . getenv('LANG') . PHP_EOL;


// http://php.net/manual/ja/function.error-reporting.php
error_reporting(E_ALL & ~E_NOTICE); // ignore out of bounds of an array.

// You can also use command line arguments:
echo "\$argc: $argc" . PHP_EOL;
echo '$argv: ';
var_dump($argv);

// Assin and check command-line-arguments easily at the same time:
$param1 = $argv[1] ? $argv[1] : die(colorize('param1 is not specified!!', 'magenta') . PHP_EOL);
$param2 = $argv[2] ? $argv[2] : die(colorize('param2 is not specified!!', 'magenta') . PHP_EOL);


function colorize($str, $color){
    $begin = "\e[";
    $end = 'm';
    /* static */ $cdic = array_flip(array('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'));
    return join(array($begin, "3", chr(0x30 + $cdic[$color]), $end,
                      $str,
                      $begin, '0', $end));
}