How can error handling be improved in PHP scripts that use getopt for processing command line arguments?

When using getopt for processing command line arguments in PHP scripts, error handling can be improved by checking for errors and providing informative messages to the user if invalid arguments are provided. This can be done by using conditional statements to check for errors returned by getopt and displaying corresponding error messages.

$options = getopt("f:l:");

if (!$options || count($options) < 2) {
    echo "Usage: php script.php -f <file> -l <line>" . PHP_EOL;
    exit(1);
}

if (!isset($options['f']) || !isset($options['l'])) {
    echo "Both -f and -l options are required." . PHP_EOL;
    exit(1);
}

$file = $options['f'];
$line = $options['l'];

// Continue processing with valid arguments