0

I want to use <?php if (statement): ?>, <?php else if (statement): ?>, and <?php else: ?> lines to make my code more readable. I'm not quite sure if this is the correct syntax. I also wonder if I can use php code within these if blocks. This is what I am working with:

<?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>
6
  • 1
    Oh. Wow. Um.... You should also move your CSS to an external stylesheet. Commented Aug 28, 2012 at 23:03
  • 4
    "to make my code more readable" - I don't think it worked! Commented Aug 28, 2012 at 23:04
  • 1
    Dude, have you thought about looking at the documentation... uk3.php.net/manual/en/control-structures.elseif.php Commented Aug 28, 2012 at 23:05
  • @trickyzter Yes I have looked at the php documentation and it says nothing about using php if statements for blocks of code '<?php if (statement): ?>' Commented Aug 28, 2012 at 23:19
  • By all means make your code more readable. Before using endif instead of brackets, please do add newlines and indenting. Commented Aug 28, 2012 at 23:21

3 Answers 3

4

In this specific case I would go for all PHP to make it readable:

<?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>';
}

Although this might just be personal preference I think this is way more readable then keep mixing opening and closing PHP mode by all those <?php ?> all over the place.

Some IMHO benefits:

  • opening and closing curly braces (although opinions differ for templating stuff, you see what you like best)
  • proper indentation
  • no opening and closing <?php ?> tags all over the place

PS

Are you sure that the posts are html entity encoded? And are you sure you really want to decode them? Most of the times you can just get away with htmlspecialchars(). And decoding before displaying is often a bad idea and may introduce XSS vulnerabilities. Not sure in you specific case though. Just a reminder to watch what you are doing :-) E.g. are you sure you want to decode it and not encode it to prevent XSS?

PPS

One final thing. You should really drop that inline CSS as Paul stated in his comment while we are busy making it more readable.

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

Comments

2

Michael,

The short answer is yes.

You do need to endif with a semicolon: <? endif; ?>

2 Comments

He also has to remove the gap between 'else if'.
@Byron Thanks for the short answer :)
0

Sometimes indentation will help make things readable (wrapped in a document to show how PHP/HTML indenting can coexist):

<?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>

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.