如果有的话,如何使用 Switch/Case 的函数?
How to use a Function of Switch/Case, if at all?
我在一个数据库中有各种产品,每个产品都有自己的价格,输出产品价格的模式是:
$product[i]["price"]
我还有各种 "product types",它们是在我的代码中手动定义的,每个都有索引号,相应地与每个产品相关:
$type1="normal"
$type2="normal"
$type3="special"
...
我想根据每个产品的价格和类型回显消息。
我的代码模式类似于:
<p> lorem <?php echo function_output($product[1]["price"],$type1)?><p>
<p> ipsum <?php echo function_output($product[2]["price"],$type2)?><p>
<p> done <?php echo function_output($product[3]["price"],$type3)?><p>
(lorem ipsum
东西意味着我在代码中有一些静态内容)。
回显行应该是:
如果价格是0
--> <span class="free">Free</span>
if price > 0
--> Echo $
sign + price $product[i]["price"]
通过default/fallback --> 不要回显任何东西
如果type
是special
,不管价格是多少 --> echo It's
special
直觉上这听起来像是用 switch
命令来处理的东西,但我真的不知道如何按照我想要的方式使用它,这可能涉及到一个函数。我只知道 switch
命令的最基本形式,我假设我的代码应该是这样的:
$my_text = function_output($product[i]["price"],$type);
switch ($product[i]["price"]) {
case $product[i]["price"]=="0":
$message = '<span class="free">Free</span>';
break;
case $product[i]["price"]>"0":
$message = '$product[i]["price"]';
break;
case $type1="special":
$message = 'It's Special';
break;
default:
$message = 'no info';
}
是的,我知道这完全是一团糟,但不知道该怎么做。
编辑:
例如,当$product[i]["price"]=50
和$type2="normal"
function_output($product[2]["price"],$type2)
应该 return: </code></p>
<p>编辑 2:</p>
<p>基本上我希望函数以类似于以下方法的方式使用:</p>
<pre><code>function example( $slug ) {
$class_map = array(
'special' => 'it's special',
'default' => 'nothing'
);
return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : $class_map[ 'default' ];
}
然后:
`example( $product[1]["price"],$type1)`
`example( $product[2]["price"],$type2)`
...
我假设价格永远不会小于零。
所以好像只有三个输出选项:"special", "free", or "price".
PHP 的 elseif
在这里可能更有效:
function function_output($price=0,$type='normal') {
if ($type == "special") {
$message = "It's Special";
} elseif ($price == 0) {
$message = '<span class="free">Free</span>';
} else {
$message = '$'.$price;
}
return $message;
}
但是您也可以使用 switch
来评估多个变量,方法是将值传递给它 true
。
如果您将来要添加更多选项,这可能会有用。
function function_output($price=0,$type='normal') {
switch (true) {
case $type == "special":
$message = "It's Special";
break;
case $price == 0:
$message = '<span class="free">Free</span>';
break;
default:
$message = '$'.$price;
}
return $message;
}
这是一个working example。
编辑
只是为了好玩,使用嵌套 ternary 运算符的简短版本:
function output($p=0,$t='normal') {
return $t=='special'?"It's Special":($p==0?'<span class="free">Free</span>':'$'.$p);
}
你用错了 switch 语句我知道。
你不能使用那样的开关。现在 switch 语句正在评估 $product[i]["price"] 的值,它永远不会是 $product[i]["shipping"]=="0"
我认为你可以做的是:
switch (true) {
case $product[i]["shipping"]=="0":
$message = '<span class="free">Free</span>';
break;
case $product[i]["price"]>"0":
$message = '$product[i]["price"]';
break;
case $type1="special":
$message = 'It's Special';
break;
default:
$message = 'no info';
}
但我个人认为在这种情况下使用 if 语句的问题很少
我在一个数据库中有各种产品,每个产品都有自己的价格,输出产品价格的模式是:
$product[i]["price"]
我还有各种 "product types",它们是在我的代码中手动定义的,每个都有索引号,相应地与每个产品相关:
$type1="normal"
$type2="normal"
$type3="special"
...
我想根据每个产品的价格和类型回显消息。
我的代码模式类似于:
<p> lorem <?php echo function_output($product[1]["price"],$type1)?><p>
<p> ipsum <?php echo function_output($product[2]["price"],$type2)?><p>
<p> done <?php echo function_output($product[3]["price"],$type3)?><p>
(lorem ipsum
东西意味着我在代码中有一些静态内容)。
回显行应该是:
如果价格是
0
--><span class="free">Free</span>
if price
> 0
--> Echo$
sign + price$product[i]["price"]
通过default/fallback --> 不要回显任何东西
如果
type
是special
,不管价格是多少 --> echoIt's special
直觉上这听起来像是用 switch
命令来处理的东西,但我真的不知道如何按照我想要的方式使用它,这可能涉及到一个函数。我只知道 switch
命令的最基本形式,我假设我的代码应该是这样的:
$my_text = function_output($product[i]["price"],$type);
switch ($product[i]["price"]) {
case $product[i]["price"]=="0":
$message = '<span class="free">Free</span>';
break;
case $product[i]["price"]>"0":
$message = '$product[i]["price"]';
break;
case $type1="special":
$message = 'It's Special';
break;
default:
$message = 'no info';
}
是的,我知道这完全是一团糟,但不知道该怎么做。
编辑:
例如,当$product[i]["price"]=50
和$type2="normal"
function_output($product[2]["price"],$type2)
应该 return: </code></p>
<p>编辑 2:</p>
<p>基本上我希望函数以类似于以下方法的方式使用:</p>
<pre><code>function example( $slug ) {
$class_map = array(
'special' => 'it's special',
'default' => 'nothing'
);
return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : $class_map[ 'default' ];
}
然后:
`example( $product[1]["price"],$type1)`
`example( $product[2]["price"],$type2)`
...
我假设价格永远不会小于零。
所以好像只有三个输出选项:"special", "free", or "price".
PHP 的 elseif
在这里可能更有效:
function function_output($price=0,$type='normal') {
if ($type == "special") {
$message = "It's Special";
} elseif ($price == 0) {
$message = '<span class="free">Free</span>';
} else {
$message = '$'.$price;
}
return $message;
}
但是您也可以使用 switch
来评估多个变量,方法是将值传递给它 true
。
如果您将来要添加更多选项,这可能会有用。
function function_output($price=0,$type='normal') {
switch (true) {
case $type == "special":
$message = "It's Special";
break;
case $price == 0:
$message = '<span class="free">Free</span>';
break;
default:
$message = '$'.$price;
}
return $message;
}
这是一个working example。
编辑
只是为了好玩,使用嵌套 ternary 运算符的简短版本:
function output($p=0,$t='normal') {
return $t=='special'?"It's Special":($p==0?'<span class="free">Free</span>':'$'.$p);
}
你用错了 switch 语句我知道。
你不能使用那样的开关。现在 switch 语句正在评估 $product[i]["price"] 的值,它永远不会是 $product[i]["shipping"]=="0"
我认为你可以做的是:
switch (true) {
case $product[i]["shipping"]=="0":
$message = '<span class="free">Free</span>';
break;
case $product[i]["price"]>"0":
$message = '$product[i]["price"]';
break;
case $type1="special":
$message = 'It's Special';
break;
default:
$message = 'no info';
}
但我个人认为在这种情况下使用 if 语句的问题很少