Useful options for PHP Command line interface

I find PHP command line interface very useful, for CRON jobs, testing and whenever apache is not necessary. Basic commands here: CLI php.net manual. In this article I’m writing some CL flags and few lines of related functions / PHP code I find useful

  • To make script interactive and read the line from the console
    $var = readline("text");
  • Prints reflection of a function (params and required values). Can be used as a guide
    php -rf json_encode
  • save highlited code into a html file
    php -s file.php > fileWithCodeHighlited.phtml
  • Display a ini setting containing “log_” (e.g. error_log)
    php -i | grep "log_"
  • Run one command (without entering in interactive mode with php -a)
    php -r "echo time();";
  • Set a INI option before executing
    php -d max_execution_time=20 ...
  • Read from STDIN (when piped)
    $handle = fopen('php://stind', 'r');
    while (!feof($handle)) {
    	$line = trim(fgets($handle));
    	if(strlen($line) > 0){
    	echo strrev($line).PHP_EOL;
    	}
    }
    fclose($handle);
  • Get options using getopt
    $arg = getopt('ab:c::') // "a" as flag "b" required, "c" optional