Convert Number to String

Discussion in 'PHP' started by onehundredandtwo, Jul 9, 2009.

  1. #1
    Hi all,

    Does anybody know how to convert a number to a string? I'm using in_array, and when I retrieve the data from the mysql database it is returned as a string and not a number.

    onehundredandtwo.
     
    onehundredandtwo, Jul 9, 2009 IP
  2. linkstraffic

    linkstraffic Well-Known Member

    Messages:
    388
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    133
    #2
    Why do you save such data in your database?
    You better change the way you insert data from your mysql db, save numbers as numbers! or as characters but in the numeric format!
     
    linkstraffic, Jul 9, 2009 IP
  3. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Huh you said "Does anybody know how to convert a number to a string?" and then "it is returned as a string and not a number." So do you want number to string or string to number and why maybe there is another way around.

    Anyway to convert int to String maybe something like:

    $mynum = (string)$mynum;

    and oppoiste for String to int like:

    $mystr = (int)$mystr;
     
    wd_2k6, Jul 9, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    You would definitely be better off using an int type field in your DB if all the data in that column is a number.
     
    jestep, Jul 9, 2009 IP
  5. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #5
    LOL. I don't think he has 'onehundredandtwo' in database. Thats his nickname :)
     
    samyak, Jul 9, 2009 IP
  6. onehundredandtwo

    onehundredandtwo Guest

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The data in the database is already stored as an integer, but it's returned as a string. I just found the intval() function and it seems to work fine. Thanks for your help anyway guys!
     
    onehundredandtwo, Jul 9, 2009 IP
  7. anthonywebs

    anthonywebs Banned

    Messages:
    657
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $string=str_ireplace("1","one",$num);
     
    anthonywebs, Jul 9, 2009 IP
    Dirty-Rockstar likes this.