php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50244 getopt() does not reset
Submitted: 2009-11-20 14:37 UTC Modified: 2010-11-24 13:01 UTC
From: craig dot marvelley at boxuk dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.3 OS: *
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: craig dot marvelley at boxuk dot com
New email:
PHP Version: OS:

 

 [2009-11-20 14:37 UTC] craig dot marvelley at boxuk dot com
Description:
------------
A bug that has been reported and fixed in the past regarding getopt not 
resetting after being called once has resurfaced.

Here's the original bug: http://bonsai.php.net/bug.php?id=29714

The same behaviour is happening in 5.3.1 - once getopt has been called, 
subsequent calls ignore options not included in the original call.



Reproduce code:
---------------
> php test.php -d hello -b world

<?php
$options = getopt("d:");
print_r($options);

$options = getopt("b:");
print_r($options);

Expected result:
----------------
Array 
(
    [d] => hello
)

Array 
(
    [b] => world
)

Actual result:
--------------
Array 
(
    [d] => hello
)

Array 
(
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-18 20:34 UTC] alexc223 at googlemail dot com
I can also confirm this on PHP 5.3.3
 [2010-11-24 12:32 UTC] jani@php.net
-Type: Feature/Change Request +Type: Bug -Package: Feature/Change Request +Package: *General Issues -Operating System: Windows XP +Operating System: * -PHP Version: 5.3.1 +PHP Version: 5.3.3
 [2010-11-24 13:01 UTC] jani@php.net
-Status: Open +Status: Bogus
 [2010-11-24 13:01 UTC] jani@php.net
It's not about resetting, it's about how getopt() works:

From the manual: "Note: The parsing of options will end at the first non-option found, anything that follows is discarded."

Your second call encounters the passed '-d hello' which you have specified as not being included in the args, thus it's an error.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 06:01:32 2025 UTC