note 127341 added to function.getopt
| From: | S2 at main dot php dot net | Date: | Tue, 26 Jul 2022 16:55:21 +0000 |
| Subject: | note 127341 added to function.getopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219060@lists.php.net to get a copy of this message | ||
As noted the options not required or optional (parameter does not accept any value) only returns the
index key with a value of FALSE when provided as an argument. That is counter-intuitive to how the
option will likely be used, so simply leverage the keys existence and reset the variable's
index for that option:
<?php
$options = getopt('', ['update', 'insert']);
$options['update'] = array_key_exists('update', $options);
$options['insert'] = array_key_exists('insert', $options);
var_dump($options);
?>
shell> php testfile.php --insert
array(2) {
["update"]=>
bool(false)
["insert"]=>
bool(true)
}
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.133 (proxied: 2603:8080:f205:fc2b:7d86:3bdc:20b9:1e9f)
----
Manual Page -- https://php.net/manual/en/function.getopt.php
Edit -- https://main.php.net/note/edit/127341
Del: integrated -- https://main.php.net/note/delete/127341/integrated
Del: useless -- https://main.php.net/note/delete/127341/useless
Del: bad code -- https://main.php.net/note/delete/127341/bad+code
Del: spam -- https://main.php.net/note/delete/127341/spam
Del: non-english -- https://main.php.net/note/delete/127341/non-english
Del: in docs -- https://main.php.net/note/delete/127341/in+docs
Del: other reasons-- https://main.php.net/note/delete/127341
Reject -- https://main.php.net/note/reject/127341
Search -- https://main.php.net/manage/user-notes.php