【问题标题】:php how to use an array to simplify codephp如何使用数组来简化代码
【发布时间】:2015-01-12 12:16:09
【问题描述】:

我有以下一段 php 代码:

<?php 
if(get_theme_mod('typography_setting')=='small') 
{
  echo '12';
} 
else if(get_theme_mod('typography_setting')=='standard') 
{
  echo '13';
} 
else if(get_theme_mod('typography_setting')=='big') 
{
  echo '14';
} 
else if(get_theme_mod('typography_setting')=='huge') 
{
  echo '15';
}
?>

基本上说,如果排版设置是小回声 12,标准 - 回声 13,大 - 回声 14,巨大 - 回声 15。

我知道这段代码可以正常工作,但我想了解如何使用数组,我想知道这段代码是否可以通过使用数组来简化?

【问题讨论】:

  • 根据你的情况应该是switch case
  • 在那里使用 in_array()

标签: php arrays if-statement echo


【解决方案1】:

不是火箭科学:

$font_sizes = array(
    'small' => 12,
    'standard' => 13,
    ...
);

$size = get_theme_mod('typography_setting');
if( isset($font_sizes[$size]) ){
    echo $font_sizes[$size];
}

您还可以通过更多地使用 Enter 键来增强您的代码。

【讨论】:

    猜你喜欢
    • 2019-08-20
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    相关资源
    最近更新 更多