www.webdeveloper.com
Results 1 to 15 of 15

Thread: convert string to int

  1. #1
    Join Date
    Mar 2007
    Posts
    195

    convert string to int

    i need to take a value from a form which is in string format and convert it to an int. i need to do a simple addidtion with the string value and then spit it back out as a string again..

  2. #2
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    PHP is a typeless language, so when you attempt to add an integer and a string, the string is automatically assumed as an integer. Although if you'd like, look into typecasting:
    PHP Code:
    $string = (int)$string
    Now the string variable is of an integer type.

  3. #3
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    number_format() is also useful for handling integer / float to string conversion.

  4. #4
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    number_format() returns a string.

    $string = (int)$string; should be used
    or
    $string = intval($string);

  5. #5
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Quote Originally Posted by NightShift58
    number_format() returns a string.

    $string = (int)$string; should be used
    or
    $string = intval($string);

    number_format() is also useful for handling integer / float to string conversion.


  6. #6
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Quote Originally Posted by ripken204
    i need to take a value from a form which is in string format and convert it to an int. i need to do a simple addidtion with the string value and then spit it back out as a string again..
    Why use number_format() for that?

  7. #7
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    You don't for the first part, I was just saying you can use number_format() to do the "spit it back out as a string" part to drop long decimals such as number_format(123.34567, 2); would give you "123.34". (round as needed)
    Last edited by MrCoder; 03-22-2007 at 06:48 AM.

  8. #8
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    How many decimals do integers have in your neck of the woods?

  9. #9
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Many people say integer when they really mean float.

  10. #10
    Join Date
    Mar 2007
    Posts
    195
    Quote Originally Posted by pcthug
    PHP is a typeless language, so when you attempt to add an integer and a string, the string is automatically assumed as an integer. Although if you'd like, look into typecasting:
    PHP Code:
    $string = (int)$string
    Now the string variable is of an integer type.
    ya i figured that one out a couple nights ago... casting, i cant beleive i forgot about it. we do it so much in java class. anyways, it works good now. it would just be nice if php had actual variable/object types, im just so used to java.

  11. #11
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    I doubt we will ever see it, one of the objectives of PHP is to be more simple per se and not require strict variable type declaration, though php is looking more and more like Java everyday, so I guess it may not be that farfetched.

  12. #12
    Join Date
    Mar 2007
    Posts
    195
    and when you think about it, php is prolly assigning int or string values to the variable without telling us. thats why i would have to cast it as an int...

  13. #13
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Quote Originally Posted by ripken204
    and when you think about it, php is prolly assigning int or string values to the variable without telling us. thats why i would have to cast it as an int...
    Of course variables are assigned types, it's just that those types are freely mutable depending on what operations are performed on them.

  14. #14
    Join Date
    Aug 2008
    Posts
    6

    php convert string to int online

    There are many different ways to convert string to int,
    e.g.

    Code:
    $input => 123
    (integer)$input => 123
    intval($input) => 123
    $input*1 => 123
    settype($input, "integer") => 123

    check: http://phpconvert.com/online/convert/stringtoint/HKYAo

  15. #15
    Join Date
    Aug 2008
    Posts
    6

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles