Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8447121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:06:30+00:00 2026-06-10T10:06:30+00:00

I want to use <?php if (statement): ?> , <?php else if (statement): ?>

  • 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:06:31+00:00Added an answer on June 10, 2026 at 10:06 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use an access card reader with PHP. I am doing this
Today I am trying to echo this php mysql statement within my javascript code,
I want to use PHP to replace javascript functions in HTML documents. For example:
I am currently using various back-end services and I want to use PHP to
Simple question I guess, I want to use PHP to write an update to
I want use a single php file to handle all of my voting requests.
I want to use the sharer.php popup to open the share dialog, but would
I use the PHP language for file upload and want to know that the
I want to use an associative array with the PHP iterator: http://php.net/manual/en/class.iterator.php is it
I want to use PDT to debug PHP with eclipse. I am using ubuntu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.