{"id":15946,"date":"2020-07-09T05:19:01","date_gmt":"2020-07-09T05:19:01","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=15946"},"modified":"2022-04-13T15:58:37","modified_gmt":"2022-04-13T15:58:37","slug":"how-to-create-alternating-color-code-snippets-in-php","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-create-alternating-color-code-snippets-in-php\/","title":{"rendered":"How to Create alternating color code snippets in PHP?"},"content":{"rendered":"\n<p>In this blog, I&#8217;m going to give you the answer to how this is created using PHP, And then as I said it&#8217;s only just a few lines of code. We&#8217;re just looking for the shortest one.<\/p>\n\n\n\n<p>So, we&#8217;ve got 10 rows there. And again really the number of rows here is not really the purpose of the exercise. We&#8217;re just looking to be able to alternate those colors.<\/p>\n\n\n\n<p>So, going to have incremented to create our loop and then within here, we&#8217;re going to apply our logic. So one of the obvious things that we&#8217;re outputting the value of pi. So we&#8217;re putting the current value of dollars. So this is one of the things that we&#8217;re outputting. And also from looking at the source code we can tell that we&#8217;ve got it within a div.<\/p>\n\n\n\n<p>So we&#8217;ve got a second time here and we&#8217;re just closing up that div. And now we&#8217;re also because we&#8217;re changing that background and we&#8217;re just using style to apply that background because remember again PHP is writing this out it&#8217;s writing that HTML so we can apply it as a style we can apply it as a class and there&#8217;s any number of different ways that we can apply this.<\/p>\n\n\n\n<p>If you&#8217;ve got multiple colors as classes that&#8217;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&#8217;re setting the background to alternating.this in PHP is that we&#8217;re using a variable here to set that value.<\/p>\n\n\n\n<p>So this gives us the ability to make that dynamic And this was a short way to do that. It&#8217;s a very useful bit of code to have because when you are listing our multiple things on a Web site.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/ajcotocus\/d48a646d6c027eb764d6e8b40badd245.js\"><\/script>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"312\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/code-snippet-in-php.png\" alt=\"\" class=\"wp-image-15983\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/code-snippet-in-php.png 765w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/code-snippet-in-php-300x122.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/figure>\n\n\n\n<p>If you want to have these alternating colors especially if you&#8217;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.<\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_94033\"><iframe loading=\"lazy\"  id=\"_ytid_90620\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_94033\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>In this blog, I&#8217;m going to give you the answer to how this is created using PHP, And then as I said it&#8217;s only just a few lines of code&#8230;. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[6224,1003,6222,350,177,5360],"class_list":["post-15946","post","type-post","status-publish","format-standard","hentry","category-php","tag-alternating","tag-code","tag-color","tag-create","tag-php","tag-snippets"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=15946"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15946\/revisions"}],"predecessor-version":[{"id":24680,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15946\/revisions\/24680"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=15946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=15946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=15946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}