PHP Variables Great Than Less Than

Robert Granlund
Robert Granlund used Ask the Experts™
on
If I have a variable that has a lot of comma separated numbers.
$var = '594,458,458,458,40,40,488,488,200,40,40,80,120,200,514,5';

And I have two other variables that I want to compare it against
$var_a = "475";
$var_b = "450";

And I want to write the following;
if($var contains a number that is Less than or equal to <= $var_a AND contains a number that is greater than or equal to $var_b) {
 $my_new_var = "TRUE";
}
I'm not surre how to write that
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try this (untested):

$var = '594,458,458,458,40,40,488,488,200,40,40,80,120,200,514,5';
$var_a = "475";
$var_b = "450";

$arr = explode(",", $var);

$a = false;
$b = false;

foreach($arr as $value) {
  if($value <= $var_a) $a = true; 
  if($value >= $var_b) $b = true; 
};

if($a && $b) $my_new_var = true;

Open in new window

HTH,
Dan

Author

Commented:
I have the following but I don't think it is correct.
It is always echoing "HELLO"
$wave = $row['wavelength'];
   			$wave = explode(', ', $wave);
   			echo'<br />'.$wave[0].'<br />'.$wave[1].'<br />';

   			$a = false;
   			$b = false; 

   			if($wave[0] <= $wave_max) $a = true; 
   			if($wave[1] >= $wave_min) $b = true; 
   			
   			if($a && $b) $wavelength = true;
   			
   			if($wavelength) {
   				echo '<h1>HELLO</h1>';
   			}		

Open in new window

Author

Commented:
Never mind my last comment.  That works well.  Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial