我有这个php块

<?php switch ($_GET['class_id']) { ?>
<?php case 1:    ?>
<img src="<?php print $result['standard_image'] == "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result['standard_image']}" ?>" alt="" />
<?php break; ?>
<?php case 2:    ?>
<img src="<?php print $result['business_image'] == "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result['business_image']}" ?>" alt="" />
<?php break; ?>
<?php case 3:    ?>
<img src="<?php print $result['premium_image'] == "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result['premium_image']}" ?>" alt="" />
<?php break; ?>
<?php default: ?>
<img src="<?php print $result['standard_image'] == "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result['standard_image']}" ?>" alt="" />
<?php break; ?>
<?php } ?>

但这看起来有点混乱..有什么建议吗

最佳答案

为什么一开始所有的案子?你只是在选择使用什么图像。

$images = array(
  1 => 'standard_image',
  2 => 'business_image',
  3 => 'premium_image'
);

$id = $_GET['class_id'];
$image_key = (isset($images[$id])) ? $images[$id] : 'standard_image';

$image = '/pre_config/css/images/blue-png2.png';
if ($result[$image_key] != '')
    $image = $result[$image_key];

echo '<img src="' . $image . '" alt="Logo" />';

额外的好处是:逻辑的目的更加清晰。

10-05 19:26