Jump to content

Having problems with is_float()


jiveturkey420

Recommended Posts

I'm having a problem with a shopping cart project I'm doing for school.

<font size="6"><b>Add a New Product</b></font><hr/>
<form method="POST" action="products.php">
<b>Name:</b> <input type="text" name="prod_name" size="25" value="" />
<b>Price:</b> <input type="text" name="prod_price" size="25" value="" /><br/>
<b>Description:</b> <input type="text" name="prod_desc" size="53" value="" /><br/>
<b>Weight</b> <i>(in lbs):</i> <input type="text" name="prod_weight" size="4" value="" />
<b>Amount in Stock:</b> <input type="text" name="prod_stock" size="5" value="" /><br/>
<input type="submit" name ="action" value="Add"><hr></form>

<?php

if ($_POST['prod_name'] >= '5') {
   echo "Product name is greater than or equal to 5<br/>"; }

if (is_float($_POST['prod_price'])) {
   echo "Product price is a float<br/>"; }

if (strlen($_POST['prod_weight']) < 4) {
   echo "Product weight is under 1,000lbs<br/>"; } 

echo $_POST['prod_price']."<br/>"; 
if(is_float($_POST['prod_name'])) {
   echo "Superglobal POST prod_name is a float"; } ?>

 

Everything works, except for the is_float() function. It wont echo out "Product price is a float".

Am I using it properly?

Link to comment
Share on other sites

Text input fields pass the data as "text". In fact, I think all POST data is passed as text except for maybe file uploads.

 

So, when you use

if (is_float($_POST['prod_price']))

 

$_POST['prod_price'] is always a string - even if the string is '1.23'. Although PHP is a loosely typed language and you could use  the string '1.23' in a match function, the is_float() function is specifically checking if the element has the value of a float AND is of a numeric type.

 

is_float()

Finds whether the type of a variable is float

 

I think you want to be using is_numeric()

Finds whether a variable is a number or a numeric string
Link to comment
Share on other sites

your problem is due to the fact that the posted data is actually a string. so even though you have put in something like 2.55 it will actually be '2.55' which is a string. you can check it is numeric using

is_numeric()

. you can also check using a regular expression. Or type cast the value into a float :

(float)$_POST['prod_price']

Link to comment
Share on other sites

you can also check using a regular expression. Or type cast the value into a float :

(float)$_POST['prod_price']

 

Regular Expression is a great tool, but it should only be used when there are no alternatives. Forcing the value to a float would work, but it means you would potentially be changing the value from what the user entered. If the user accidentally entered "1.2.3" and really meant to enter "1.23", then forcing the value to a float would end up with "1.2". I would rather capture the error and send back to the user to correct.

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.