<?php
function next_arg(array $argv): string {
    $i = 1;
    $arg = '-d';
    $next = function () use ($argv, &$i, $arg): string {
        if (!isset($argv[$a = $i + 1])) {
            exit(2);
        }
        $i = $a;
        return $argv[$i];
    };

    return $next();
}

echo next_arg(["script", "-d", "/dev/tty"]), PHP_EOL;
