1

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?

2
  • as per your condition it should be switch case Commented Jan 12, 2015 at 12:21
  • use in_array() there Commented Jan 12, 2015 at 12:23

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.