Jump to content

Trouble with numbers and >= using strings


erich1

Recommended Posts

I am drawing text from a web resource.  It is numbers about water levels.  I then want that string in this case $high_level checked against a variety of numbers which represent high and low water levels.  So basically the text is highlighted a different color depending on the water level.  Right now as far as I can tell $high_level=73.15  which is greater than 50 and should show us the color yellow and not red.  I tried trim in case there was a problem with white space.  I dont know what to do...

 

<?php
$file = "http://www.environment.alberta.ca/forecasting/data/hydro/tables/BOW-RHIWDIE-WL.txt";
$handle = @fopen($file, "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle);
    }
    fclose($handle);
	$year = date('Y');
	$high_level = strrchr($buffer, ' ');
    $date = substr(strstr($buffer, $year), 0, 16);
}
?>
<img src="http://www.paddlingabc.com/images/img05.jpg" alt="arrow"> <a target="_blank" href="http://www.environment.alberta.ca/forecasting/data/hydro/tables/BOW-RHIWDIE-WL.txt">Highwood @ Diebel's Ranch:</a><br />
<?php 
trim ($high_level);
echo $high_level;
if ($high_level<='15') {
echo "<span style=\"background-color: #FF3300\">";
echo $high_level;
echo "cms, ";
echo $date;
echo "</span>";
}
elseif ($high_level<='25') {
echo "<span style=\"background-color: #FFFF66\">";
echo $high_level;
echo "cms, ";
echo $date;
echo "</span>";
}
elseif ($high_level>='50') {
echo "<span style=\"background-color: #FFFF66\">";
echo $high_level;
echo "cms, ";
echo $date;
echo "</span>";
}
else {
echo "<span style=\"background-color: #33FF00\">";
echo $high_level;
echo "cms, ";
echo $date;
echo "</span>";
}
if ($highlevel>='50'){
echo 'Higher';
}
else{
echo 'error';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/180643-trouble-with-numbers-and-using-strings/
Share on other sites

firstly, just use numbers, not numbers inside quotes. so instead of this

if ($highlevel>='50'){

do this

if ($highlevel>=50){

 

for all the line that are like that. you may want to try using intval() on the variable also, IE

if (intval($highlevel)>=50){

 

Hope that helps

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.