How would I go about converting this if statement:
如何转换if语句:
for($i = 1; $i < $argc; $i++)
{
...
if(in_array($argv[$i], array('-V', '--version')))
{
$displayVersion = TRUE;
}
...
}
Into a switch case without needing to write two switch statements?
不需要写两个交换语句就可以转换成交换情况?
4 个解决方案
#1
13
switch($argv[$i])
{
case '-V':
case '--version':
$displayVersion = true;
break;
}
#2
5
A direct translation would be as follows:
直接翻译如下:
switch(in_array($argv[$i], array('-V', '--version'))){
case true:
$displayVersion = TRUE; break;
}
However you could also do something like this, which is clearer.
但是你也可以这样做,这样更清楚。
switch($argv[$i]){
case '-V':
case '--version':
$displayVersion = TRUE; break;
}
Depending on what you want to do, a one liner may be more clear, although it differs from the above code in that the variable will be set to false if in_array($argv[$i], array('-V', '--version'))
is false. Given your variable name I doubt this is a bad thing.
根据您想要做的,一行代码可能更清晰,尽管它与上面的代码不同,因为如果in_array($argv[$i], array('-V', '- version')是假的,那么变量将被设置为false。考虑到你的变量名,我怀疑这是件坏事。
$displayVersion = in_array($argv[$i], array('-V', '--version'));
#3
3
switch ($argv[$i])
{
case '-V':
case '--version':
$displayVersion = true;
break;
case 'other':
// do other stuff
break;
default:
// your "else" case would go here
break:
}
#4
2
In addition to fixing this, you may want to look at the PHP getopt command, which is a function to process command-line arguments in both short and long formats.
除了解决这个问题之外,您可能还需要查看PHP getopt命令,它是一个处理短格式和长格式命令行参数的函数。
Edit: Actually, here's a code block
编辑:实际上,这是一个代码块
$options = getopt('V', array('version'));
if ($options['V'] || $options['version']) {
$displayVersion = TRUE;
}
Note that you need PHP 5.3 for this to work on Windows.
注意,需要PHP 5.3才能在Windows上工作。
#1
13
switch($argv[$i])
{
case '-V':
case '--version':
$displayVersion = true;
break;
}
#2
5
A direct translation would be as follows:
直接翻译如下:
switch(in_array($argv[$i], array('-V', '--version'))){
case true:
$displayVersion = TRUE; break;
}
However you could also do something like this, which is clearer.
但是你也可以这样做,这样更清楚。
switch($argv[$i]){
case '-V':
case '--version':
$displayVersion = TRUE; break;
}
Depending on what you want to do, a one liner may be more clear, although it differs from the above code in that the variable will be set to false if in_array($argv[$i], array('-V', '--version'))
is false. Given your variable name I doubt this is a bad thing.
根据您想要做的,一行代码可能更清晰,尽管它与上面的代码不同,因为如果in_array($argv[$i], array('-V', '- version')是假的,那么变量将被设置为false。考虑到你的变量名,我怀疑这是件坏事。
$displayVersion = in_array($argv[$i], array('-V', '--version'));
#3
3
switch ($argv[$i])
{
case '-V':
case '--version':
$displayVersion = true;
break;
case 'other':
// do other stuff
break;
default:
// your "else" case would go here
break:
}
#4
2
In addition to fixing this, you may want to look at the PHP getopt command, which is a function to process command-line arguments in both short and long formats.
除了解决这个问题之外,您可能还需要查看PHP getopt命令,它是一个处理短格式和长格式命令行参数的函数。
Edit: Actually, here's a code block
编辑:实际上,这是一个代码块
$options = getopt('V', array('version'));
if ($options['V'] || $options['version']) {
$displayVersion = TRUE;
}
Note that you need PHP 5.3 for this to work on Windows.
注意,需要PHP 5.3才能在Windows上工作。