简体   繁体   中英

How can I simplify this compound php if/else statement

I have three possible ways in which I can set the height of the 'hero' section of a given page, where there's a level of prioritisation of which variable should be chosen.

If the variable is set at the page level, then use that.

If it's a blog archive or blog post, then use the blog options settings.

If they're not available, use the theme settings.

if they're not available, use the default.

If it's not a blog, then use the theme settings if available, otherwise, use the hard-coded default.

Here's how I've written it so far:

//// CUSTOMIZER OPTIONS
    $heroHeightDefault = 'header_md';
    $heroHeightTheme = get_theme_mod( 'hero_header_height', 0 );
    $heroHeightBlog = get_field($sepPrefix . '_override_height', 'options');
    $heroHeightPage = get_field($sepPrefix . '_override_height');
    
    if(empty($heroHeightTheme) && empty($heroHeightBlog) && empty($heroHeightPage)){
        $heroHeight = $heroHeightDefault; // No settings - choose default (this is my safety in case my logic below is flawed)
    } elseif(!empty($heroHeightPage)){
        $heroHeight = $heroHeightPage; // if the page settings are NOT empty choose the page settings
    } elseif($isBlogPage) { // if the page settings are empty Check to see if it's a blog page
        if(!empty($heroHeightBlog)){
            $heroHeight = $heroHeightBlog; // If the blog options are set, use the blog options
        } elseif(!empty($heroHeightTheme)){
            $heroHeight = $heroHeightTheme; // if the blog options were not set, and the theme options are not set, use the default
        } else {
            $heroHeight = $heroHeightDefault; // use the default if no settings for the blog page
        }
    } elseif(!empty($heroHeightTheme)) {
        $heroHeight = $heroHeightTheme; // If nothing else has been set, check the theme settings and use that
    } else {
        $heroHeight = $heroHeightDefault; // Otherwise, use the default.
    }

This works, and it works and does what I want it to, but I'm not sure if there's a better, more efficient way to get to the same thing - any advice would be appreciated!

This is much shorter and should yeild you the same results. Its a little less readable...

if(trim($heroHeightTheme.$heroHeightBlog.$heroHeightPage) === '') $heroHeight = $heroHeightDefault; 
elseif(!empty($heroHeightPage)) $heroHeight = $heroHeightPage; 
elseif($isBlogPage)  $heroHeight = !empty($heroHeightBlog) ? $heroHeightBlog : (!empty($heroHeightTheme) ? $heroHeightTheme : $heroHeightDefault); 
else $heroHeight = !empty($heroHeightTheme) ? $heroHeightTheme : $heroHeightDefault; 

a little easier to decipher is to separate out the conditionals from the logic

 $no_settings = trim($heroHeightTheme.$heroHeightBlog.$heroHeightPage) === '' ;
 $blogHeight = !empty($heroHeightBlog) ? $heroHeightBlog : (!empty($heroHeightTheme) ? $heroHeightTheme : $heroHeightDefault);
 $themeHeight = !empty($heroHeightTheme) ? $heroHeightTheme : $heroHeightDefault;

 if($no_settings) $heroHeight = $heroHeightDefault; 
 elseif(!empty($heroHeightPage)) $heroHeight = $heroHeightPage; 
 elseif($isBlogPage) $heroHeight = $blogHeight; 
 else $heroHeight = $themeHeight; 

I'm a big fan of wrapping that kind of logic up in a function. Once you've separated it out, you can reason about what is happening without having to worry about interaction with other variables, and subsequent refactoring gets a lot easier. For example:

function getHeroHeightEmpty($prefix, $isBlogPage)
{
    $page = get_field($prefix . '_override_height');
    $blog = get_field($prefix . '_override_height', 'options');
    $theme = get_theme_mod('hero_header_height', 0);
    $default = 'header_md';

    $order = $isBlogPage ?
    [$page, $blog, $theme, $default] :
    [$page, $theme, $default];

    while (empty($order[0])) {
        array_shift($order);
    }

    return $order[0];
}

If you are using PHP 7, and the values returned by the get_* methods are not just empty() , but null , you can clean that up even more with:

function getHeroHeightNull($prefix, $isBlogPage)
{
    $page = get_field($prefix . '_override_height');
    $blog = get_field($prefix . '_override_height', 'options');
    $theme = get_theme_mod('hero_header_height', 0);
    $default = 'header_md';

    if($isBlogPage) {
        return $page ?? $blog ?? $theme ?? $default;
    }

    return $page ?? $theme ?? $default;
}

Similar to the above, you can also use the Elvis operator , which should perform correctly when presented with non-null, empty values:

function getHeroHeightElvis($prefix, $isBlogPage)
{
    $page = get_field($prefix . '_override_height');
    $blog = get_field($prefix . '_override_height', 'options');
    $theme = get_theme_mod('hero_header_height', 0); 
    $default = 'header_md';

    if($isBlogPage) {
        return $page ?: $blog ?: $theme ?: $default;
    }   
    return $page ?: $theme ?: $default;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM