Jump to content

variable conparison tests


truegilly

Recommended Posts

Hi

was wondering is anyone could shed any light on this no doute simple problem.

lets say i have some variables, [color=red]$Actual [/color]and [color=red]$Target[/color].

[code]$Target = 5;
$Actual = 3;[/code]

Both are integers and get their values from a database.

see the example below its all very simple

[code]if ($Target == $Actual)
{
$colour = "green";
return $colour;
}
else if ($Actual < $Target)
{
$colour = "red";
return $colour;
}
else
{
$Actual > $Target;
$colour = "blue";
return $colour;
}[/code]

what i would like to add to this statement is if $actual is less than the $Target by 2, it will return "Amber".

so the expression will be something like this...

[code]
if ($Actual < $Target by 2) {
return "amber"
}
[/code]

can anyone explain how this is done ?

thanks in advance  :P

Truegilly
Link to comment
https://forums.phpfreaks.com/topic/35653-variable-conparison-tests/
Share on other sites

Suzzane2020 has given you the only way you can achieve this...

You may also use

if($Target==$Actual-2) or if($Target+2==$Actual)... hehehehehehe :p... but the point is that I don't think there's any other real solution.

[quote]
if(($Target-$Actual)==2)
{
  $color="Amber";
  return $color;
}
[/quote]
thanks for your help,

heres my code. the problem is is the red section seem to take over and doesn't allow the amber to show, could you adjust my code so it will work ?

[code] if ($Target == $Actual)
{
$colour = "green";
return $colour;
}
if(($Actual-$Target)==2)
{
  $color="Amber";
  return $color;
}
else if ($Actual < $Target)
{
$colour = "red";
return $colour;
}
else
{
$Actual > $Target;
$colour = "blue";
return $colour;
}[/code]

thanks

Truegilly    :D
Hi guys,

thanks for all your help. Its now working and the code is below for anyone who has a similar problem

[code] if ($Target == $Actual)
{
$colour = "green";
return $colour;
}
else if(($Target-$Actual)==2 || ($Target-$Actual)==1)
{
  $color="Amber";
  return $color;
}
else if ($Actual < $Target)
{
$colour = "red";
        return $colour;
}
else
{
$Actual > $Target;
$colour = "blue";
return $colour;
}[/code]

Truegilly  :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.