haris244808 Posted September 14, 2012 Share Posted September 14, 2012 Hi there, im trying to validate an input if its float or not... As im getting the value with $_POST['smth']; the value its considered as string, so is_float() function doesnt work. I tried to convert the string also with floatval() function that doesnt work either Is there any suggestion on how to get done with this?? THNX Quote Link to comment https://forums.phpfreaks.com/topic/268371-check-is-the-input-value-is-a-double-float-value/ Share on other sites More sharing options...
ManiacDan Posted September 14, 2012 Share Posted September 14, 2012 As im getting the value with $_POST['smth']; the value its considered as string, so is_float() function doesnt work. I tried to convert the string also with floatval() function that doesnt work either What makes you think this? That's exactly the purpose of those functions: php > $a = "12.34"; php > var_dump(is_float($a)); bool(false) php > var_dump(floatval($a)); float(12.34) php > var_dump(floatval($a) == $a); bool(true) php > Quote Link to comment https://forums.phpfreaks.com/topic/268371-check-is-the-input-value-is-a-double-float-value/#findComment-1377867 Share on other sites More sharing options...
haris244808 Posted September 14, 2012 Author Share Posted September 14, 2012 As im getting the value with $_POST['smth']; the value its considered as string, so is_float() function doesnt work. I tried to convert the string also with floatval() function that doesnt work either What makes you think this? That's exactly the purpose of those functions: php > $a = "12.34"; php > var_dump(is_float($a)); bool(false) php > var_dump(floatval($a)); float(12.34) php > var_dump(floatval($a) == $a); bool(true) php > yes i know thaose functions works well .. But as im getting input with $_POST[]... it seems that it gets as a string so when i check, it doesn work : here is the code: if(!is_float($this->place_latitude) || !is_float($this->place_longitude)){ $this->errors[] = "The Latitude and Longitude must be a number(double)"; return false; } here is the html form for only lattitude: <td><label>Longitude: </label></td> <td><input name="p_latitude" type="text" placeholder="Place latitude"></td> and i get it with: $newPlace->place_latitude = trim($_POST['p_latitude']); so when i type a numer (ex: 12321) which in this case is an integer. It returns true instead of showing the error mesage (returning false) Quote Link to comment https://forums.phpfreaks.com/topic/268371-check-is-the-input-value-is-a-double-float-value/#findComment-1377868 Share on other sites More sharing options...
Psycho Posted September 14, 2012 Share Posted September 14, 2012 yes i know thaose functions works well .. But as im getting input with $_POST[]... it seems that it gets as a string so when i check, it doesn work : Please reread ManiacDan's response. Don't just skim over it - really read it. And pay particular attention to the code examples. Quote Link to comment https://forums.phpfreaks.com/topic/268371-check-is-the-input-value-is-a-double-float-value/#findComment-1377874 Share on other sites More sharing options...
haris244808 Posted September 14, 2012 Author Share Posted September 14, 2012 yes i know thaose functions works well .. But as im getting input with $_POST[]... it seems that it gets as a string so when i check, it doesn work : Please reread ManiacDan's response. Don't just skim over it - really read it. And pay particular attention to the code examples. i red it well man: php > $a = "12.34"; php > var_dump(is_float($a)); bool(false) as u can see if there is a float in string qoute it returns false... This is what im saying...even when u write a float in ur input gettinh it with$_POST[] , it gets as a string so it doesnt work... if u dont believe try the code i gave to u? or try this for simplicity: $test = "132.3"; if(!is_float($test)){ echo " Fail";} else{ echo "Pass";} it seems that i frist need to convert the string to float than check it Quote Link to comment https://forums.phpfreaks.com/topic/268371-check-is-the-input-value-is-a-double-float-value/#findComment-1377875 Share on other sites More sharing options...
ManiacDan Posted September 14, 2012 Share Posted September 14, 2012 This is what im saying...even when u write a float in ur input gettinh it with$_POST[] , it gets as a string so it doesnt work... if u dont believe try the code i gave to u? [...] it seems that i frist need to convert the string to float than check it We know. We know very very well. That's why my code example was more than one line. It was, in fact, three lines. One of those lines was how you can check to see if a string is a valid floatvalue. also note the existence of is_numeric(), which works on strings. Also also note that latitude and longitude are correctly expressed in minutes and seconds, as in -64'38" Quote Link to comment https://forums.phpfreaks.com/topic/268371-check-is-the-input-value-is-a-double-float-value/#findComment-1377878 Share on other sites More sharing options...
haris244808 Posted September 14, 2012 Author Share Posted September 14, 2012 This is what im saying...even when u write a float in ur input gettinh it with$_POST[] , it gets as a string so it doesnt work... if u dont believe try the code i gave to u? [...] it seems that i frist need to convert the string to float than check it We know. We know very very well. That's why my code example was more than one line. It was, in fact, three lines. One of those lines was how you can check to see if a string is a valid floatvalue. also note the existence of is_numeric(), which works on strings. Also also note that latitude and longitude are correctly expressed in minutes and seconds, as in -64'38" yeah yeah i know that ur code was in three lines: $test = "132.3"; if(var_dump(floatval($test)) != $test){ echo " Fail";} else{ echo "Pass";} is_numeric() it just checks if its a number or not, so in integers also itll return true (i need to check if its double ) for lat and long im getting double values to display the propper postion in map Quote Link to comment https://forums.phpfreaks.com/topic/268371-check-is-the-input-value-is-a-double-float-value/#findComment-1377880 Share on other sites More sharing options...
ManiacDan Posted September 14, 2012 Share Posted September 14, 2012 is_numeric() it just checks if its a number or not, so in integers also itll return true (i need to check if its double ) Check the third line. Try to understand what it's doing. Combine that with a similar check for intval. Also note that your code shouldn't contain var_dump, var_dump is an output function that I used to show you results. Quote Link to comment https://forums.phpfreaks.com/topic/268371-check-is-the-input-value-is-a-double-float-value/#findComment-1377886 Share on other sites More sharing options...
Psycho Posted September 14, 2012 Share Posted September 14, 2012 haris244808, Have you ever heard about the saying Give a man a fish and feed him for a day. Teach a man how to fish and feed him for a lifetime. A big part of this forum, IMO, is not just providing an answer but providing information that helps users to understand how to think differently about problems they face to understand how to solve problems on their own in the future. You have been given the solution you need. It just wasn't spoon fed to you. Also, every integer is a float, so in addition to the first solution ManiacDan provided you need to pay attention to his last post as well. Quote Link to comment https://forums.phpfreaks.com/topic/268371-check-is-the-input-value-is-a-double-float-value/#findComment-1377930 Share on other sites More sharing options...
ManiacDan Posted September 14, 2012 Share Posted September 14, 2012 Also, every integer is a float, so in addition to the first solution ManiacDan provided you need to pay attention to his last post as well. Integers are not floats internally. Declaring $a = 12 makes $a an int, not a float. Quote Link to comment https://forums.phpfreaks.com/topic/268371-check-is-the-input-value-is-a-double-float-value/#findComment-1377933 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.