php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76347 getopt() only usable once
Submitted: 2018-05-16 08:42 UTC Modified: 2018-05-24 13:08 UTC
From: kaishos at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: PHP options/info functions
PHP Version: 7.0.30 OS: ubuntu0.16.04.1
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kaishos at gmail dot com
New email:
PHP Version: OS:

 

 [2018-05-16 08:42 UTC] kaishos at gmail dot com
Description:
------------
This affects php 7.0.28 that is default packages with ubuntu, could not find it in the list

seems that it will not parse more options if you have parsed one.

it has similarity with:
https://bugs.php.net/bug.php?id=35594

Test script:
---------------
create file called test.php and add:

<?php
    $fromOpt = getopt("f:");
    $toOpt = getopt("t:");

    print_r($fromOpt);
    print_r($toOpt);

?>
https://bugs.php.net/bug.php?id=35594&edit=1

then run: 
php test.php -f -t





Expected result:
----------------
Array
(
    [f] =>
)
Array
(
    [t] =>
)

Actual result:
--------------
Array
(
    [f] =>
)
Array
(
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-16 21:36 UTC] cmb@php.net
-Package: *General Issues +Package: PHP options/info functions
 [2018-05-23 21:20 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2018-05-23 21:20 UTC] cmb@php.net
Firstly, active suport support for PHP 7.0 ended five months
ago[1], so this branch will only receive security related fixes.

Secondly, the expected result is actually:

  Array
  (
      [f] => -t
  )
  Array
  (
  )

See <http://php.net/manual/en/function.getopt.php>.

If you really get the reported actual result with an actively
supported PHP version, this would be a bug.  Please check again.

[1] <http://php.net/supported-versions.php>
 [2018-05-24 08:13 UTC] kaishos at gmail dot com
-Status: Feedback +Status: Assigned
 [2018-05-24 08:13 UTC] kaishos at gmail dot com
Well then I know, as this worked before we upgraded to 7.0, strange that existing functionality was removed then
 [2018-05-24 08:53 UTC] cmb@php.net
-Status: Assigned +Status: Feedback
 [2018-05-24 08:53 UTC] cmb@php.net
From what I can tell, no functionality has been removed. At least,
I get the same result with PHP 5.6.32, for instance.  And this
result is to be expected, since f: looks for an option with a
required argument (which is -t in this case), and t: looks for an
option with a required argument which is not there.  Compare that
to:

  php test.php -ffoo -tbar

  =>

  Array
  (
      [f] => foo
      [t] => bar
  )
  Array
  (
      [t] => bar
 )

Which is also expected behavior.


So again, do you get differing results?
 [2018-05-24 12:33 UTC] kaishos at gmail dot com
-Status: Feedback +Status: Assigned
 [2018-05-24 12:33 UTC] kaishos at gmail dot com
Ah yes, now I see that I did wrong, it was that there should be no space between arguments -f and -t and their values for it work as you pointed out.

-ffoo -tbar
  Array
  (
      [f] => foo
  )
  Array
  (
      [t] => bar
 )

-ffoo -t bar (this works)
  Array
  (
      [f] => foo
  )
  Array
  (
      [t] => bar
 )

-f foo -tbar
Array
(
    [f] => foo
)

Array
(
)


-f foo -t bar
Array
(
    [f] => foo
)

Array
(
)
 [2018-05-24 13:08 UTC] cmb@php.net
-Status: Assigned +Status: Not a bug
 [2018-05-24 13:08 UTC] cmb@php.net
The last two examples also behave as expected, since -f is no
valid option for getopt('t:'), so “foo” is not regarded as
argument of the -f option, but rather as non-option, which
terminates further processing of options.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 06:01:28 2024 UTC