php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #46359 getopt fails to recognize '?' as a short option
Submitted: 2008-10-21 20:25 UTC Modified: 2009-11-20 12:41 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: johnston dot joshua at gmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: OS X 10.5.x and OpenSuSE
Private report: No CVE-ID: None
 [2008-10-21 20:25 UTC] johnston dot joshua at gmail dot com
Description:
------------
getopt() accepts a "?" character as a short option but does not return it if it is present in argv. It doesn't say anywhere in the manual about ? being a special character or which characters are accepted. It is common practice to use -h/-? as options that mean show the help

I don't know if this is a documentation error or function error.

Reproduce code:
---------------
<?php
var_dump(getopt('?'));



Expected result:
----------------
jjohnston@devws37:~> php test.php -?
array(0) {
  ["?"]=>
  bool(false)
}

Actual result:
--------------
jjohnston@devws37:~> php test.php -?
array(0) {
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-22 10:18 UTC] RQuadling at GMail dot com
The parse_opts() function in php-src/ext/standard/basic_functions.c 
is responsible for this issue.

Only 0-9, a-z and A-Z are acceptable characters for short options.

It is odd that --? is supported if '?' is a long option.

This 1 line change patch below should cover it.

Index: basic_functions.c
===================================================================
RCS file: /repository/php-src/ext/standard/basic_functions.c,v
retrieving revision 1.952
diff -u -r1.952 basic_functions.c
--- basic_functions.c	26 Mar 2009 20:02:28 -0000	1.952
+++ basic_functions.c	22 Apr 2009 10:17:31 -0000
@@ -4125,6 +4125,7 @@
 	memset(paras, 0, sizeof(opt_struct) * count);
 	*result = paras;
 	while ( (*opts >= 48 && *opts <= 57) || /* 0 - 9 */
+			(*opts == 63) || /* ? */
 			(*opts >= 65 && *opts <= 90) || /* A - Z */
 			(*opts >= 97 && *opts <= 122)   /* a - z */
 	) {
 [2009-04-22 10:23 UTC] RQuadling at GMail dot com
Also, if the short options list is something like ...

'h?r'

the 'r' option is not allowed.

<?php
$options = getopt('h?r');
?>

outputs ...

Array
(
)

when using ...

php testopt.php -r
 [2009-11-20 12:41 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2009-11-20 12:41 UTC] svn@php.net
Automatic comment from SVN on behalf of vrana
Revision: http://svn.php.net/viewvc/?view=revision&revision=291076
Log: Short options allows only a-zA-Z0-9 (bug #46359)
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Mon Jun 15 02:00:02 2026 UTC