Sam Skirrow
Sam Skirrow

Reputation: 3697

php how to use an array to simplify code

I have the following piece of php code:

<?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';
}
?>

Essentially saying, if typography setting is small echo 12, standard - echo 13, big - echo 14, huge - echo 15.

I know this code works fine, but I'm wanting to learn about using arrays and I'm wondering if this code can be simplified by using an array?

Upvotes: 1

Views: 85

Answers (1)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146578

Not rocket science:

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

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

You can also enhance your code with a more profuse use of the Enter key.

Upvotes: 6

Related Questions