php-if、else-if和if在同样包含php语句的代码块上


php if, else if, and if on blocks of code that also contain php statements

我想使用<?php if (statement): ?><?php else if (statement): ?><?php else: ?>行来提高代码的可读性。我不太确定这是否是正确的语法。我还想知道我是否可以在这些if块中使用php代码。这就是我正在使用的:

<?php $blogexcerpt = $Blog->create_excerpt(html_entity_decode($post->content), 0, 250); ?>
<!-- if the blogexcerpt contains less than 5 non-white spaces assume there is media and use the "View Media" tag -->
<?php if ( strlen(preg_replace( '/'s+/', ' ', $blogexcerpt)) < 5 ): ?>
<h5 style="margin:0px;"><a href="<?php echo $Blog->get_blog_url('post').$post->slug; ?>" class="blog_post_link">View Media…</a></h5>
<!-- else if the blog post is less than excerpt length post entire -->
<?php else if ( strlen($Blog->create_excerpt(html_entity_decode($post->content), 0, 255)) < 250 ): ?>
<?php echo $blogexcerpt; ?>
<h5 style="margin:0px;"><a href="<?php echo $Blog->get_blog_url('post').$post->slug; ?>" class="blog_post_link">View post</a></h5>
<!-- else show excerpt -->
<?php else: ?>
<?php echo $blogexcerpt; ?><span style="display: inline;">…</span>
<h5 style="margin:0px;"><a href="<?php echo $Blog->get_blog_url('post').$post->slug; ?>" class="blog_post_link">Read more…</a></h5>
<?php endif>

在这种特定的情况下,我会使用所有的PHP来使其可读:

<?php
$blogexcerpt = $Blog->create_excerpt(html_entity_decode($post->content), 0, 250);
if ( strlen(preg_replace( '/'s+/', ' ', $blogexcerpt)) < 5 ) {
    echo '<h5 style="margin:0px;"><a href="' . $Blog->get_blog_url('post').$post->slug; . '" class="blog_post_link">View Media…</a></h5>';
} else if ( strlen($Blog->create_excerpt(html_entity_decode($post->content), 0, 255)) < 250 ) {
    echo $blogexcerpt;
    echo '<h5 style="margin:0px;"><a href="' . $Blog->get_blog_url('post').$post->slug; . '" class="blog_post_link">View post</a></h5>';
} else {
    echo $blogexcerpt;
    echo '<span style="display: inline;">…</span>';
    echo '<h5 style="margin:0px;"><a href="' . $Blog->get_blog_url('post').$post->slug; . '" class="blog_post_link">Read more…</a></h5>';
}

虽然这可能只是个人的偏好,但我认为这比所有<?php ?>在所有地方混合打开和关闭PHP模式更具可读性。

IMHO的一些好处:

  • 打开和关闭大括号(尽管对于模板化的东西有不同的意见,但您可以看到自己最喜欢的内容)
  • 适当压痕
  • 到处都没有打开和关闭<?php ?>标签

PS

你确定帖子是html实体编码的吗?你确定你真的想解码它们吗?大多数情况下,htmlspecialchars()是可以逃脱惩罚的。在显示之前进行解码通常是个坏主意,并且可能会引入XSS漏洞。但不确定你的具体情况。只是提醒你注意你在做什么:-)例如,你确定要解码它而不是编码它以防止XSS吗?

PPS

最后一件事。正如Paul在他的评论中所说,当我们忙于使其更可读时,您真的应该放弃内联CSS。

Michael,

简短的回答是肯定的。

您确实需要用分号结束:<? endif; ?>

有时缩进有助于使内容可读(封装在文档中以显示PHP/HTML缩进如何共存):

<?php
$blogexcerpt = $Blog->create_excerpt(html_entity_decode($post->content), 0, 250);
?>
<html>
    <body>
<?php if ( strlen(preg_replace( '/'s+/', ' ', $blogexcerpt)) < 5 ): ?>
        <h5 style="margin:0px;"><a href="<?php echo $Blog->get_blog_url('post').$post->slug; ?>" class="blog_post_link">View Media…</a></h5>
<?php elseif ( strlen($Blog->create_excerpt(html_entity_decode($post->content), 0, 255)) < 250 ): ?>
        <?php echo $blogexcerpt; ?>
        <h5 style="margin:0px;"><a href="<?php echo $Blog->get_blog_url('post').$post->slug; ?>" class="blog_post_link">View post</a></h5>
<?php else: ?>
        <?php echo $blogexcerpt; ?><span style="display: inline;">…</span>
        <h5 style="margin:0px;"><a href="<?php echo $Blog->get_blog_url('post').$post->slug; ?>" class="blog_post_link">Read more…</a></h5>
<?php endif; ?>
    </body>
</html>