ksmatthews Posted August 16, 2008 Share Posted August 16, 2008 HI all, I would like to know how to correctly compare numbers. For example, I have 2 vars $x and $y that both contain varchars from a DB dump. If $x = 9 and $y = 10, obviously $x < $y BUT when I compare them it seems to suggest the $x > $y How can I convert them into INTS or indeed FLOATS, if I needed them ? I always thought that this was automatic in PHP. It certainly is NOT in the C language where you would need to use atoi(). regards, Steven M Link to comment https://forums.phpfreaks.com/topic/119950-comparing-numbers/ Share on other sites More sharing options...
ratcateme Posted August 16, 2008 Share Posted August 16, 2008 i am not sure as i have never used it before but intval() might help Scott. Link to comment https://forums.phpfreaks.com/topic/119950-comparing-numbers/#findComment-617932 Share on other sites More sharing options...
thebadbad Posted August 16, 2008 Share Posted August 16, 2008 You can type cast the variables. Link to comment https://forums.phpfreaks.com/topic/119950-comparing-numbers/#findComment-617934 Share on other sites More sharing options...
gerkintrigg Posted August 16, 2008 Share Posted August 16, 2008 from my days as a maths student, you need to bring them down to a common denominator. I suppose it may be that i just have no idea what you're trying to do, but they both need to be playing with the same format. if you want a float, you can use something like number_format($number, 2) That then sets it to 2 decimal places. is that what you're after? Link to comment https://forums.phpfreaks.com/topic/119950-comparing-numbers/#findComment-617995 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.