How to Read Program Arguments in PHP

In PHP the program arguments of current script is always available in the $argv variable except when it is not.

This means we can easily write the old hello world app as:

<?php
if(count($argv)<2) {
  echo "usage: php $argv[0] name [name] [name] [name]";
}
foreach($argv as $index=>$arg) {
  // skip index zero as this is the script name
  if($index==0) continue;
  echo "Hello $arg\n";
}

Related Posts