In this blog, I’m going to give you the answer to how this is created using PHP, And then as I said it’s only just a few lines of code. We’re just looking for the shortest one.
So, we’ve got 10 rows there. And again really the number of rows here is not really the purpose of the exercise. We’re just looking to be able to alternate those colors.
So, going to have incremented to create our loop and then within here, we’re going to apply our logic. So one of the obvious things that we’re outputting the value of pi. So we’re putting the current value of dollars. So this is one of the things that we’re outputting. And also from looking at the source code we can tell that we’ve got it within a div.
So we’ve got a second time here and we’re just closing up that div. And now we’re also because we’re changing that background and we’re just using style to apply that background because remember again PHP is writing this out it’s writing that HTML so we can apply it as a style we can apply it as a class and there’s any number of different ways that we can apply this.
If you’ve got multiple colors as classes that’s available to us we can just write that out. So this is just writing that each team code right from the server. So this is how we apply a style and inline style and we’re setting the background to alternating.this in PHP is that we’re using a variable here to set that value.
So this gives us the ability to make that dynamic And this was a short way to do that. It’s a very useful bit of code to have because when you are listing our multiple things on a Web site.

If you want to have these alternating colors especially if you’re creating these types of lists. So this is a great way to do that. And also if you want to update it every three lines changing we could do something like that as well.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals