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

  • Home
  • SEARCH
  • 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 5839907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:39:32+00:00 2026-05-22T11:39:32+00:00

Something in PHP, I want to display content if a certain variable exists. For

  • 0

Something in PHP, I want to display content if a certain variable exists.

For example, For the current comment I want to check if the author has already voted on this reply, so the code will be something as the following:

<?php if (isset($replies)): ?>
    <?php foreach ($replies as $reply): ?>

        <?php if (isset($already_voted)): ?>
            <?php foreach ($already_voted as $vote): ?>

            // if vote['id'] == $reply['id'], echo what they have already voted

            <?php endforeach; ?>
        <?php endif; ?>

    <?php endforeach; ?>
<?php endif; ?>

What would be an easier way of doing this, without using a bloated templating engine like smarty?

(Please, no comments about… yes ‘PHP is essentially a templating engine’ or anything along those lines).

  • 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-05-22T11:39:33+00:00Added an answer on May 22, 2026 at 11:39 am

    I would recommend using a templating engine for templates (I recommend PHPTAL), but you have asked very specifically for a method to not use an engine. You can simplify the php code you have a bit just by not breaking out of it so much:

    <?php
    if (isset($replies)) {
       foreach ($replies as $reply) {
    
          if (isset($already_voted)) {
             foreach ($already_voted as $vote) {
    
        // if vote['id'] == $reply['id'], echo what they have already voted
    
             }
          }
    
       }
    }
    

    Of course, this is practically the same thing as what you have, it just uses fewer characters. Keep track of open code blocks via indentation.

    Note that the point of a template, at least according to my understanding, is reuse. You want to be able to use the same template, or at least portions of a template (macros for example) across as many pages as possible. For some static content, this may not always be a possibility, but that is why the key words are as possible. As you should do with any code, reuse what you can.

    Using a template system makes it easier to separate view from logic. You can create php templates (as you have done), but I think it lacks clarity and it is far easier to do processing within the template rather than before. It’s also more difficult to create reusable templates without any sort of system in php [citation needed]. You can create your own that works for you, but there are already some great ones out there.

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

Sidebar

Related Questions

want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
How do I get a time countdown using PHP? I want to display something
I want to define something like this in php : $EL = \n<br />\n;
I want to do something like this: (in php) $a = class_name1; $b =
I'm pretty proficient in PHP, but want to try something new. I'm also know
is it possible, that in php something, which was written as comment( like //comment...
Using PHP, can I post content to a Facebook page when I add something
I have my site under the structure: www.somesite.com/index.php?page=p_section_page.php I want my site to display
I want some content to only be printed in my node-type.tpl.php if the node
I want to force the reader's email client to display my php-generated plain text

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.