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

PHP cookies

Today we are going to learn about cookies in PHP. Cookies are like files or better say content that the website/server would embed on your computer through your browser. ...

12 Min Read Read More
PHP Array Flip
Feb 14, 2023 programming

PHP Array Flip

Today we are going to talk about array flip function in PHP. Array flip is very simple yet useful array function that helps you flip and array. ...

4 Min Read Read More
PHP Simple Web Crawler
Mar 22, 2023 programming

PHP Simple Web Crawler

Today we are going to build a simple web crawler in PHP and parse the box office movies from IMDB. We can do it with PHP easily. We don’t even need any extra package. ...

14 Min Read Read More
PHP Callback Functions
Jan 08, 2023 programming

PHP Callback Functions

Today we are going to talk about callback functions in PHP. sometimes we need to pass a function as an argument of another function, that's exactly what callback function is. ...

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