I still can't figure it all out

I have got some good help earlier and hopefully you also can help me with this one. Probably a simple solution but I struggle.
The code below works fine on the $data_from_external_source till 9.9 m/s
but from 10.0 m/s it won’t “play”. Any Suggestion on what I do wrong here? ???

[php]

<?php $a = '0.2 m/s'; $b = '1.5 m/s'; $c = '3.3 m/s'; $data_from_external_source = '10.0 m/s'; if($data_from_external_source <= $a) { echo "text 1 ($data_from_external_source)"; } if($data_from_external_source >$a && $ data_from_external_source <=$b) { echo "text 2 ($data_from_external_source)"; } if($data_from_external_source > $b && $ data_from_external_source <=$c) { echo "text 3 ($data_from_external_source)"; } else { echo 'text4'; } ?>

[/php]

You’re comparing strings, not numbers. I.e. this string ‘3.3 m/s’ is greater than this string ‘10.0 m/s’
If you want correct results, you need to compare numbers, and get rid of m/s in all calculations/comparison, then append ’ m/s’ when you’re outputting result to user.

I was afraid that could be the reason. Problem is that I get this m/s from an external source and can’t do anything about it unless there is a code to put in to clean it out.

In php it is very easy to work with strings. If your data is always formatted like in your example (i.e. [decimal][space]m/s), then you can just split these strings by space, and use the first part - numeric value.

Here is the code:
[php]list($a, ) = explode(’ ‘, ‘0.2 m/s’);
list($b, ) = explode(’ ‘, ‘1.5 m/s’);
list($c, ) = explode(’ ', ‘3.3 m/s’);

list($data_from_external_source, ) = explode(’ ', ‘10.0 m/s’);

// leave the rest of your code as is
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service