|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #81352 Doc states getopt returns false when it really returns an empty array
Submitted: 2021-08-12 12:26 UTC Modified: 2021-08-12 15:19 UTC
From: edg at greenberg dot org Assigned:
Status: Open Package: PHP options/info functions
PHP Version: 7.4.22 OS: Centos 7
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: edg at greenberg dot org
New email:
PHP Version: OS:


 [2021-08-12 12:26 UTC] edg at greenberg dot org
From manual page:

ON the manual page for getopt it states:

> This function will return an array of option / argument pairs, or false on failure.

When there are no options, it returns an empty array.  

So my bug report is to suggest that you indicate that "false on failure" refers to some other kind of failure and that no options found is indicated by an empty array. 


Test script:
$options = getopt('', ['help', 'limit:', 'loops:', 'endtime:', 'tomorrow', 'agency:']);

Expected result:
Based on the doc, if I call the script with no options, I expected failure. 

Actual result:
I got an empty array.  

Note that I'm quite happy with the empty array, but wish that things in the doc had been a bit less ambiguous.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-12 15:19 UTC]
-Package: Documentation problem +Package: PHP options/info functions
 [2021-08-12 15:19 UTC]
Prior to PHP 8.0.0, FALSE was returned if the function was called
with unexpected parameters or parameter types.  Most other
functions returned NULL in that case.  Anyhow, there is no need to
document that, since this is actually undefined behavior[1].

For all relevant PHP versions, FALSE will also be returned, if
neither $_SERVER nor $argv exists or are not arrays.

[1] <>
 [2022-11-26 06:42 UTC] schamberumarcelo at gmail dot com
getopt() will return an empty array if there is no error in splitting strings to args variable. GetOptions() will return a true value if the command line could be processed successfully. Otherwise, it will write error messages using die() and warn(). (
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 18 12:01:29 2024 UTC