We help IT Professionals succeed at work.
Get Started

Better way to write this PHP

jabberwok_e asked
Last Modified: 2012-06-21
I'm looking for a more efficient way to write this PHP code. Here is a portion of it to give you the idea. It covers the 12 months in the calendar year all done in if/else statements. I know there has to be a more efficient way of doing this
$date = date("n");
      if (1 <= $date){
            echo '<img src="img/january_c_m.jpg" width="157" height="56" alt="January" />';}
      else{echo '<img src="img/january_b_m.jpg" width="157" height="56" alt="January" />';}
      if (2 <= $date){
            echo '<img src="img/feburary_c_m.jpg" width="157" height="56" alt="February" />';}
      else{echo '<img src="img/feburary_b_m.jpg" width="157" height="56" alt="February" />';}
      if (3 <= $date){
            echo '<img src="img/march_c_m.jpg" width="157" height="56" alt="March"  />';}
      else{echo '<img src="img/march_b_m.jpg" width="157" height="56" alt="March" />';}
      if (4 <= $date){
            echo '<img src="img/april_c_m.jpg" width="157" height="56" alt="April"  />';}
      else{echo '<img src="img/april_b_m.jpg" width="157" height="56" alt="April"  />';}
   etc etc....

Basically I have a calendar page that a visitor can come to. A check is performed to see what month it is and only show color images for that month and any prior months. All other months ahead are to be  black and white images.

So the PHP code does the check and echos out either a color or a black and white image depending on the month when the visitor's visits.

Any help is much appreciated.

Watch Question
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE