有没有更好的方法来设计这个PHP函数


Is there a better way to design this PHP function?

它有效,但也许你们中的某个人知道是否有更好的方法来实现我想要做的事情:

<?php if ($md_options->mobileversion == true) {;?><!-- Ok Mobile mode is ON -->
    <?php if (detectdevice() != 'true') {;?><!-- Now if we are NOT on a mobile let's start! -->
        <?php if ((is_single()) && ($metaBox == true)) {;?><!-- If is Single and 360Panorama is set let's show panorama -->
            <?php include('360ok.php');?><!-- Houston we have a panorama! -->
        <?php } elseif ((!is_single()) || ($metaBox != true)){;?><!-- Each page that hasn't set a panorama will check... -->
            <?php if ($md_options->slideorvideo == 'true') {;?><!-- Do you want to show a slideshow? -->
                <?php include('slideshowhome.php');?><!-- What a nice slideshow! -->
            <?php } elseif ($md_options->slideorvideo == 'false') {;?><!-- You prefer a video? let's show it! -->
                <?php include("video/video.php");?>
            <?php };?><!-- Closes video option -->
            <?php if ($md_options->slivideoposition != 'center'){;?><!-- Let's show the post next to the video or slideshow if it is possible -->
                <?php include('postheader.php');?>
            <?php };?><!-- Closes POSTHEADER -->
        <?php };?><!-- Close Else if !is_single || $metabox != true -->
    <?php };?><!-- Closes the function that checks if we are on a mobile or not -->
<?php } elseif ($md_options->mobileversion != true) {;?><!-- Closes mobile version ON/OFF -->
    <?php if ((is_single()) && ($metaBox == true)) {;?><!-- If is Single and 360Panorama is set let's show panorama -->
        <?php include('360ok.php');?><!-- Houston we have a panorama! -->
    <?php } elseif ((!is_single()) || ($metaBox != true)){;?><!-- Each page that hasn't set a panorama will check... -->
        <?php if ($md_options->slideorvideo == 'true') {;?><!-- Do you want to show a slideshow? -->
            <?php include('slideshowhome.php');?><!-- What a nice slideshow! -->
        <?php } elseif ($md_options->slideorvideo == 'false') {;?><!-- You prefer a video? let's show it! -->
            <?php include("video/video.php");?>
        <?php };?><!-- Closes video option -->
        <?php if ($md_options->slivideoposition != 'center'){;?><!-- Let's show the post next to the video or slideshow if it is possible -->
            <?php include('postheader.php');?>
        <?php };?><!-- Closes POSTHEADER -->
    <?php };?><!-- Close Else if !is_single || $metabox != true -->
<?php };?><!-- If mobile mode is OFF this closes the IF -->

相同的代码(没有无关的<?php ?>标签(:

<?php
if ($md_options->mobileversion == true) {
    if (detectdevice() != 'true') {
        if ((is_single()) && ($metaBox == true)) {
            include('360ok.php')
        } elseif ((!is_single()) || ($metaBox != true)) {
            if ($md_options->slideorvideo == 'true') {
                include('slideshowhome.php');
            } elseif ($md_options->slideorvideo == 'false') {
                include("video/video.php");
            }
            if ($md_options->slivideoposition != 'center') {
                include('postheader.php');
            }
        }
    }
} elseif ($md_options->mobileversion != true) {
    if ((is_single()) && ($metaBox == true)) {
        include('360ok.php');
    } elseif ((!is_single()) || ($metaBox != true)) {
        if ($md_options->slideorvideo == 'true') {
            include('slideshowhome.php');
        } elseif ($md_options->slideorvideo == 'false') {
            include("video/video.php");
        }
        if ($md_options->slivideoposition != 'center') {
            include('postheader.php');
        }
    }
}
?>

有没有更快的方法到达同一点?

首先,?>关闭标记意味着;。因此,写入?>类似于写入; ?>。在它之前添加另一个;将类似于写入;; ?>

你可以做很多事情来改善这一点。

  • 卸下<?php ... ?>。只需打开一个<?php标签,并在写入整个逻辑后用?>关闭它
  • if (something) { ... } else { ... }替换if (something == true) { ... } else if (something != true) { ... }逻辑
  • 删除无关的括号
  • 将逻辑提取到组件中,并尝试简化它
  • 只需存储返回值,就可以停止反复调用同一个函数

据我所知,该代码的目的是在某些情况下包含其中一个:"360ok.php"、"slidehome.php"或"video/video.php"answers"post-header.php"。你可以把逻辑建立在这个基础上。

简化逻辑使其可读性更强:

<?php
if ($md_options->mobileversion) {
    if (detectdevice() != 'true') {
        if (is_single() && $metaBox) {
            include('360ok.php');
        } else {
            if ($md_options->slideorvideo == 'true') {
                include('slideshowhome.php');
            } elseif ($md_options->slideorvideo == 'false') {
                include('video/video.php');
            }
            if ($md_options->slivideoposition != 'center') {
                include('postheader.php');
            }
        }
    }
} else {
    if (is_single() && $metaBox) {
        include('360ok.php');
    } else {
        if ($md_options->slideorvideo == 'true') {
            include('slideshowhome.php');
        } elseif ($md_options->slideorvideo == 'false') {
            include('video/video.php');
        }
        if ($md_options->slivideoposition != 'center') {
            include('postheader.php');
        }
    }
}
?>

由此我了解到有两种情况:

  • 如果$md_options->mobileversiondetectdevice() != 'true'
  • 如果不是CCD_ 15

在每种情况下,逻辑都是相同的,所以我们可以进一步简化它:

if (!$md_options->mobileversion || detectdevice() != 'true') {
    if (is_single() && $metaBox) {
        include('360ok.php');
    } else {
        if ($md_options->slideorvideo == 'true') {
            include('slideshowhome.php');
        } elseif ($md_options->slideorvideo == 'false') {
            include('video/video.php');
        }
        if ($md_options->slivideoposition != 'center') {
            include('postheader.php');
        }
    }
}

此外,如果$md_options->slideorvideo的值可以是字符串'true''false',那么我们可以进一步简化它:

if (!$md_options->mobileversion || detectdevice() != 'true') {
    if (is_single() && $metaBox) {
        include('360ok.php');
    } else {
        if ($md_options->slideorvideo == 'true') {
            include('slideshowhome.php');
        } else {
            include('video/video.php');
        }
        if ($md_options->slivideoposition != 'center') {
            include('postheader.php');
        }
    }
}

清晰简洁的代码通常比过于冗长的代码更具可读性和可维护性。此外,没有理由不能将注释作为PHP注释包括在内,而只能包含提示为什么要包含某个文件的相关注释。例如:

// only do this if mobile mode is ON and we're not on a mobile,
// or if mobile mode is OFF
if (!$md_options->mobileversion || detectdevice() != 'true') {
    if (is_single() && $metaBox) {
        // if is single and 360 panorama is set, let's show panorama
        include('360ok.php');
    } else {
        // show the slideshow or the video, according to the user's preferences
        if ($md_options->slideorvideo == 'true') {
            include('slideshowhome.php');
        } else {
            include('video/video.php');
        }
        // if possible, let's also show the post next to it
        if ($md_options->slivideoposition != 'center') {
            include('postheader.php');
        }
    }
}

正如您所看到的,实际上没有必要在else旁边添加额外的注释,因为您可以流利地阅读代码和相关注释。任何额外的评论都会妨碍阅读,并降低重要评论的可读性。

改进代码的两个简单方法:

  • 如果HTML注释属于PHP代码,请将其转换为PHP注释
  • 从在HTML中对PHP代码进行缩进转换为在PHP中只进行缩进。这也应该移除所有多余的<?php?>标签