How to Create alternating color code snippets in PHP?

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.

Ajay K
Follow me
Total Page Visits: 72 - Today Page Visits: 0