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>
In this specific case I would go for all PHP to make it readable:
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:
<?php ?>tags all over the placePS
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.