Jump to content


Photo

converting int/float/char -> String


  • Please log in to reply
5 replies to this topic

#1 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 08 June 2006 - 07:45 PM


    /* $formObj is not known here */
     if(is_int($formObj)){
       $this->formObj = sprintf("%d", $formObj);
     }else if(is_float($formObj)){
       $this->formObj = sprintf("%f", $formObj);
     else{
        $this->formObj = $formObj;
     }
    /* $this->formObj is guarenteed to be a string at this point */
[code]

My question is... is there a function already that does this?

[code]
    /* $formObj is not known here */
   $this->formObj = thefunctioniwant($formObj);
    /* $this->formObj is guarenteed to be a string at this point */

Sure i already wrote a basic version of it... but there are types i'm not checking (yet) like "bool" etc...
I just want to know if there is something that will always returning a string equivalent of
whatever was passed into it. I'll invent the wheel again if it hasn't been done...

Chris

#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 08 June 2006 - 08:37 PM

[a href=\"http://us3.php.net/manual/en/function.strval.php\" target=\"_blank\"]strval()[/a]

NoGray.com


#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 08 June 2006 - 09:08 PM

Or use typecasting, like (string) $variable
~ D Kuang

#4 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 02:20 AM

wouldn't sticking quotes "" around the variable do the trick as well?
[table]



Don't worry, the printer fairies will sort it out.

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 09 June 2006 - 06:29 PM

Yes, it would cast it as a string.

There is also settype() function
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 11 June 2006 - 11:45 PM

Thanks everyone, thats exactly what i was looking for!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users