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 Operators
Mar 21, 2023 programming

PHP Operators

Today we are going to learn about Operators in PHP including arithmetic, comparison and logic operators. You will be using all of these operators during your career a lot. ...

5 Min Read Read More
PHP Conditionals
Mar 21, 2023 programming

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
PHP Loops
Mar 21, 2023 programming

PHP Loops

Today we are going to learn about loops in PHP. Loop is another important concept in programming and we are going to learn everything about loops in PHP. ...

11 Min Read Read More
PHP function
Mar 21, 2023 programming

PHP function

Today we are going to learn about functions in PHP. Functions are very important because they can help you divide your application in small bites that can be handled with a function. ...

10 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 ...