Free cookie consent management tool by TermsFeed Generator 3 Ways to add Conditional and Loop within HTML | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
3 Ways to add Conditional and Loop within HTML

3 Ways to add Conditional and Loop within HTML

Last Updated on Mar 21, 2023

Introduction

Loops and Conditionals are two pieces of code that we keep writing so it's good to make sure our code is readable. When we write PHP withing HML it becomes even more important. Now we are going to learn about different ways that we can write PHP conditionals and loops within HTML and I will share the advantages and disadvantages of each one.

First let's see what we are going to do.

we have an array of names:

$names = ['pratham','simon','favor','oliver','amir'];

We want to write this list as a HTML list. so the final HTML code would look like this:

<ul>
    <li>pratham</li>
    <li>simon</li>
    <li>favor</li>
    <li>oliver</li>
    <li>amir</li>
</ul>

so let's start writing the loop in different ways.

First Way

The first possible way is to write the loop and echo the list items like string.

<?php
foreach($names as $name){
    echo '<li>' . $name . '</li>';
}
?>

the issue with this is that in a lot of IDEs if I write my code like this, I will not get autocompletion and tag closure and other useful features that I would get when I'll be writing pure HTML. so very soon it gets tricky.

Second Way

the second way is somehow a solution to the problem of the first way. so instead of echoing the HTML tags we will keep them outside the PHP code. like this:

<?php foreach($names as $name){ ?>
<li> <?php echo $name ?> </li>
<?php } ?>

this way we can enjoy all the features that our IDE offers for HTML and we still use the power of PHP. but the problem with this way is the last line:

<?php } ?>

Just a single closing bracket! for now for a simple example like this we know exactly what this is for but imaging a code that you have a loop and inside the loop you have a conditional and so on. very soon you will have 10 single brackets and you have no idea what they are for. and the solution for that is the third way.

Third Way

in this way, instad of using the typical opening and closing brackets for our loops and conditions we use the : and endfor and endif structure. so instead of writing:

<?php foreach($names as $name){ ?>

<?php } ?>

we will write

<?php foreach($names as $name):  ?>

<?php endforeach; ?>

and in case of our example the full code would look like this

<?php foreach($names as $name):  ?>
<li> <?php echo $name ?> </li>
<?php endforeach; ?>

this way even if we have 10 loops and conditions we know that one is for ending the loop and one is for ending the conditionals etc. it's much more readable and much more maintainable.

https://youtu.be/fwKmUrGOKqY

Conclusion

Now you know how to add PHP conditional and loops within HML.

I recommend you to open a PHP files and try to practice all the different ways to be able to see the advantages and disadvantages of each one.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • Writing PHP Loops within HTML
  • Writing PHP Conditionals within HTML
  • Readable Code
  • Maintainable Code

Category: programming

Tags: #php #tips and tricks

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP array map
Feb 14, 2023 programming

PHP array map

Today we are going to learn about array map function in PHP. It lets you apply a function to the elements of the given arrays and returns an array with changed elements. ...

4 Min Read Read More
PHP string functions
Mar 21, 2023 programming

PHP string functions

Today we are going to learn about some of the useful string functions in PHP. we are going to send a text through different functions and learn what those functions do. ...

9 Min Read Read More
Creating a Simple Jupyter Notebook in PHP
Aug 28, 2024 programming

Creating a Simple Jupyter Notebook in PHP

This tutorial will guide you through the steps to create a simple PHP-based notebook interface. The notebook allows you to write and run PHP code in a web browser, maintaining the state between code executions. ...

28 Min Read Read More
Alpine.js Beginner to Advanced 2025: Everything You Need to Know
Jul 18, 2025 programming

Alpine.js Beginner to Advanced 2025: Everything You Need to Know

JavaScript frameworks have exploded in popularity over the past decade, but not every project needs—or benefits from—the complexity of tools like React or Vue. Sometimes, all you want is to sprinkle interactivity into your HTML without a heavy build process. This is exactly where Alpine.js shines. ...

29 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...