php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #23859 getopts() indicates set options using FALSE
Submitted: 2003-05-28 10:45 UTC Modified: 2003-05-28 20:45 UTC
From: csnyder at chxo dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.3.1 OS: Linux / FreeBSD
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
1 + 12 = ?
Subscribe to this entry?

 
 [2003-05-28 10:45 UTC] csnyder at chxo dot com
The current behavior of getopts() is counter-intuitive.

From the manual:
"If an option does not have an argument, the value will be set to FALSE."

But this leads to a script like the following:

#!/usr/local/bin/php
<?
$options = getopt("abcdef");
if ($options['e']===FALSE) print "I took e!";
?>

This is confusing -- we test for the presence of an option in the command line by checking whether it is FALSE?

It would be quite helpful, at least in cases where no additional value was expected (an option string of "a" vs. an option string of "a:") for getopts to set the value to TRUE. The script above could then be:

#!/usr/local/bin/php
<?
$options = getopt("abcdef");
if ($options['e']) print "I took e!";
?>

...which is much easier to figure out.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-28 20:45 UTC] sniper@php.net
Changing this would break backwards compatibility.
And btw. this is much more intuitive:

#!/usr/local/bin/php
<?php
$options = getopt("abcdef");
if (isset($options['e'])) print "I took e!";
?>



 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 16:01:29 2024 UTC