Jump to content

Check is the input value is a double (float) value


haris244808

Recommended Posts

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

Link to comment
Share on other sites

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 > 

 

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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"

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.