How would I go about converting this if statement:
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?
PHP allows you to use number, character, string, as well as functions in switch expression.
This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed.
The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
The logical OR operator (||) will not work in a switch case as one might think, only the first argument will be considered at execution time.
switch($argv[$i])
{
case '-V':
case '--version':
$displayVersion = true;
break;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With