PHP - 将变量添加到 switch 语句值
PHP - Add variable to switch statement value
我有以下 switch 语句:
$html = '<div class="'. $some_value . '">';
switch ($some_value) {
case "one":
return $html . 'One Biscuit</div>';
break;
case "two":
return $html . 'Two Chimps</div>';
break;
case "three":
return $html . 'Three Pies</div>';
break;
default:
return $html . 'Meh...</div>';
}
注意到我是如何为每个案例添加 $html
变量的吗?不好...是否可以只将它添加到 switch 语句的最终值中一次?我正在尝试将最终值包装在动态 HTML 中。
一种方法:
$html = '<div class="'. $some_value . '">';
$v = 'Meh...</div>';
switch ($some_value) {
case "one":
$v = 'One Biscuit</div>';
break;
case "two":
$v = 'Two Chimps</div>';
break;
case "three":
$v = 'Three Pies</div>';
break;
}
$html .= $v;
由于您使用的是 return
,所以您最后只能 return 一次:
return $html.$v
此外,您可以定义一个参数为默认值,如下所示:
function someFunction(DUNNO_YOUR_PARAMS, $v = 'Meh...'){
$v .= '</div'>;
// rest of code
将字符串存储在新变量中。此外,您不需要在 之后 一个 return 语句。
$html = '<div class="'. $some_value . '">';
$str = null;
switch ($some_value) {
case "one":
$str = 'One Biscuit</div>';
break;
case "two":
$str = 'Two Chimps</div>';
break;
case "three":
$str = 'Three Pies</div>';
break;
default:
$str = 'Meh...</div>';
break;
}
return $html.$str;
这个怎么样:
switch($some_value){
case 'one':
$var="One Biscuit";
break;
case 'two':
$var="Two Chimps";
break;
case 'three':
$var="Three Pies";
break;
default:
$var="Meh...";
break;
}
$html="<div class=".$some_value.">".$var."</div>";
其他方法是将数据保存在数组中:
$some_value = 'two';
//
$data = array(//this data could be stored in a database table
'one' => 'One Biscuit',
'two' => 'Two Chimps',
'three'=> 'Three Pies',
'default' => 'Meh...'
);
$html = '<div class="'.$some_value.'">'.(isset($data[$some_value])?$data[$some_value]:$data['default']).'</div>';
var_dump($html);
结果:
string '<div class="two">Two Chimps</div>' (length=33)
在某些情况下数组比开关快:In PHP what's faster, big Switch statement, or Array key lookup
另一种解决方案,如果您的 html 更长,可能更易于维护:
<?php
ob_start();
echo '<div class="'.$some_var.'">';
/*
Be sure the file exists in the location you want
You can change items to the name of any directory you want just be
sure the file exists in the end. In the case of linux be sure the file
is the exact same name...
*/
include('items/'.$some_var.'.php');
echo '</div>';
$output = ob_get_clean();
return $output;
?>
在您的文件中,您只需输入您想要的 html 代码。
示例 three.php(它实际上只是 html 或纯文本,除非您处理某些内容:
Three pies
如果您有太多如此简单的文件,这可能会被重构。但如果情况更复杂,您可以包含更多内容。
我有以下 switch 语句:
$html = '<div class="'. $some_value . '">';
switch ($some_value) {
case "one":
return $html . 'One Biscuit</div>';
break;
case "two":
return $html . 'Two Chimps</div>';
break;
case "three":
return $html . 'Three Pies</div>';
break;
default:
return $html . 'Meh...</div>';
}
注意到我是如何为每个案例添加 $html
变量的吗?不好...是否可以只将它添加到 switch 语句的最终值中一次?我正在尝试将最终值包装在动态 HTML 中。
一种方法:
$html = '<div class="'. $some_value . '">';
$v = 'Meh...</div>';
switch ($some_value) {
case "one":
$v = 'One Biscuit</div>';
break;
case "two":
$v = 'Two Chimps</div>';
break;
case "three":
$v = 'Three Pies</div>';
break;
}
$html .= $v;
由于您使用的是 return
,所以您最后只能 return 一次:
return $html.$v
此外,您可以定义一个参数为默认值,如下所示:
function someFunction(DUNNO_YOUR_PARAMS, $v = 'Meh...'){
$v .= '</div'>;
// rest of code
将字符串存储在新变量中。此外,您不需要在 之后 一个 return 语句。
$html = '<div class="'. $some_value . '">';
$str = null;
switch ($some_value) {
case "one":
$str = 'One Biscuit</div>';
break;
case "two":
$str = 'Two Chimps</div>';
break;
case "three":
$str = 'Three Pies</div>';
break;
default:
$str = 'Meh...</div>';
break;
}
return $html.$str;
这个怎么样:
switch($some_value){
case 'one':
$var="One Biscuit";
break;
case 'two':
$var="Two Chimps";
break;
case 'three':
$var="Three Pies";
break;
default:
$var="Meh...";
break;
}
$html="<div class=".$some_value.">".$var."</div>";
其他方法是将数据保存在数组中:
$some_value = 'two';
//
$data = array(//this data could be stored in a database table
'one' => 'One Biscuit',
'two' => 'Two Chimps',
'three'=> 'Three Pies',
'default' => 'Meh...'
);
$html = '<div class="'.$some_value.'">'.(isset($data[$some_value])?$data[$some_value]:$data['default']).'</div>';
var_dump($html);
结果:
string '<div class="two">Two Chimps</div>' (length=33)
在某些情况下数组比开关快:In PHP what's faster, big Switch statement, or Array key lookup
另一种解决方案,如果您的 html 更长,可能更易于维护:
<?php
ob_start();
echo '<div class="'.$some_var.'">';
/*
Be sure the file exists in the location you want
You can change items to the name of any directory you want just be
sure the file exists in the end. In the case of linux be sure the file
is the exact same name...
*/
include('items/'.$some_var.'.php');
echo '</div>';
$output = ob_get_clean();
return $output;
?>
在您的文件中,您只需输入您想要的 html 代码。
示例 three.php(它实际上只是 html 或纯文本,除非您处理某些内容:
Three pies
如果您有太多如此简单的文件,这可能会被重构。但如果情况更复杂,您可以包含更多内容。