它有效,但也许有人知道是否有更好的方法来实现我想要做的事情:
<?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');
}
}
}
?>
是否有更快的方式来达到同一点?
答案 0 :(得分:7)
首先,?>
关闭标记意味着;
。因此,撰写?>
与撰写; ?>
类似。在它之前添加另一个;
类似于编写;; ?>
。
你可以做很多事情来改善它。
<?php ... ?>
。只需打开<?php
标记,然后在写完整个逻辑后用?>
将其关闭。if (something == true) { ... } else if (something != true) { ... }
逻辑替换为if (something) { ... } else { ... }
从我可以收集的内容来看,代码的目的是在某些情况下包括以下内容之一:“360ok.php”,“slideshowhome.php”或“video / video.php”和“postheader.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->mobileversion
和detectdevice() != 'true'
$md_options->mobileversion
。在每种情况下,逻辑都是相同的,因此我们可以进一步简化:
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
旁边的额外评论确实不需要,因为您可以流畅地阅读代码和相关评论。任何额外的评论都会妨碍重要评论的可读性。
答案 1 :(得分:2)
改进代码的两个简单方法:
<?php
和?>
标记。