Page 1 of 1

Php - New varaiable

Posted: Mon 31 Dec 2012 6:37 pm
by beelzebubs
I'm try to get a picture to show up when a certain color is shown, but how do I convert the results to a new variable?
I don't want to print it out right away, I want it to end up in a new variable that I can use later.

Let say it all end up with the variable $test
If the color is #ffffff the variable $test will show the picture error.gif
But if the color is #fe0104 the variable $test will show the picture varning3.gif instead.

Code: Select all

if ($WarnColor=="#ffffff")
  {
  echo "<img src='Varningar/error.gif'>";
  }
elseif ($WarnColor=="#29d660")
  {
  echo "img src='Varningar/ingen_varning.gif'";
  }
elseif ($WarnColor=="#ffff00")
  {
  echo "<img src='Varningar/varning1.gif'>";
  }
elseif ($WarnColor=="#fecb31")
  {
  echo "<img src='Varningar/varning2.gif'>";
  }
elseif ($WarnColor=="#fe0104")
  {
  echo "<img src='Varningar/varning3.gif'>";
  }
else
  {
  echo "Something has gone terribly wrong - time to shut down";
  }

Re: Php - New varaiable

Posted: Mon 31 Dec 2012 6:41 pm
by steve
Replace "echo" with "$test=", then later do "echo $test".

Re: Php - New varaiable

Posted: Mon 31 Dec 2012 7:49 pm
by beelzebubs
Thanks Steve, and have a happy new year!
Regards Kenneth

Re: Php - New varaiable

Posted: Mon 31 Dec 2012 8:47 pm
by Karv
as a side note, use of switch() is probably better suited to this.

Code: Select all

switch($WarnColor){
    case "#ffffff" :
        $test='Varningar/error.gif';
    break;

    case "#29d660" : 
        $test = 'Varningar/ingen_varning.gif';
    break;
(... etc etc ...)

    case #fecb31" : // you can have multiples to give one result:
    case #gecb32" :
    case #hecb33" : 
        $test = 'Varningar/varning3.gif';
    break;

    default :  // this is for when nothing above matches
        $test = 'very_wrong.gif';
    break;
}