Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: is_float

  1. #1
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts

    is_float

    I am trying to validate a float. I need to test if a string variable is a float. i tried the is_float function but it doesnt seem to work with string variables

    PHP Code:
    $duration=$_POST['duration'];

    if(!
    is_float($duration))
    {
        die (
    '<div style="clear:both;float:left;"><h3>Sorry your duration must be a number in hours.</h3>
             <table><tr><td>Click here to go back -</td><td> <a href="?area=1" class="button" >Back </a></td></tr></table></div></div>'
    );

    You can not say you know how to do something, until you can teach it to someone else.

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    is_float doesn't work with strings.

    Try converting $duration to an integer first using the intval() function.

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Hi thanks for the suggestion. However will converting my string in to an interger truncate the decimal digits? As the user may have inputed 1.5 as the duration.
    You can not say you know how to do something, until you can teach it to someone else.

  • #4
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    You can use floatval() to convert it to a float.

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    floatval() will turn anything (scalar) into a float, so probably not very useful:
    PHP Code:
    var_dump(is_float(floatval('hello')));
    //output=bool(true) 
    will is_numeric() not do what you want? (http://php.net/is-numeric)
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #6
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Thanks GJay thats the code i was looking for and now i look its right there on php.net silly me thanks guys :-)
    You can not say you know how to do something, until you can teach it to someone else.

  • #7
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Hi thanks for the suggestion. However will converting my string in to an interger truncate the decimal digits? As the user may have inputed 1.5 as the duration.
    oh my bad, I was thinking of int as meaning any number value, sorry!

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •