views:

41481

answers:

8

Is there a way to convert integers to string in PHP?

+2  A: 

$foo = 5;

$foo = $foo . "";

now $foo is a string.

But, you may want to get used to casting. As casting is the proper way to accomplish something of that sort.

$foo = 5;    
$foo = (string)$foo;

Another way is to encapsulate in quotes:

$foo = 5;
$foo = "$foo"
Sev
So incredibly strange that we both picked 5. Also, + is java.
karim79
Wow. Agreed. And thanks ;)
Sev
I think you're mixing up your concatenation syntax between languages there.
jason
Made the mistake and then fixed it 25 seconds later. Sorry. Gosh :)
Sev
I'd lean toward casting, simply because it makes your intentions abundantly clear. Casting has one and only one purpose: to change types. The other examples may almost look like mistakes, in some contexts.
Frank Farmer
+4  A: 

There's many ways to do this.

Two examples:

 $str = (string) $int;
 $str = "$int";

See the PHP Manual on Types Juggling for more.

Sanjay Sheth
A: 

You can either use the period operator and concatenate a string to it (and it will be type casted to a string):

$integer = 93;
$stringedInt = $integer."";

Or, more correctly, you can just type cast the integer to a string:

$integer = 93;
$stringedInt = (string) $integer;
Andrew Dunkman
+16  A: 

You can use the strval() function to convert a number to a string.

From a maintenance perspective its obvious what you are trying to do rather than some of the other more esoteric answers. Of course, it depends on your context.

$var = 5;
echo "I'd like {$var} waffles"; // = "I'd like 5 waffles

$var_to_string = mysql_escape_string($var);
$query = "SELECT * FROM `table` WHERE `id`='{$var_to_string}'";
$query = "SELECT * FROM `table` WHERE `id`='".mysql_escape_string($var)."'"; // using concat

$items = (string)$var; // $items == "5";
$items = strval($var); // $items == "5";

Those should all give the same results.

Chris Thompson
Please consider editing out the obvious potential SQL Injection holes...
gahooa
Good point. I threw in some mysql_escape_string functions in there to clean it up.
Chris Thompson
A: 

I would say it depends on the context. strval() or the casting operator (string) could be used, however in most cases PHP will decide whats good for you, if for example you use it with echo or printf... One small note: die() needs a string and won't show any int :)

merkuro
+1  A: 

There are a number of ways to "convert" an integer to a string in PHP.

The traditional CS way would be to cast the variable as a string

$int = 5;
$int_as_string = (string) $int;
echo $int . ' is a '. gettype($int) . "\n"; 
echo $int_as_string . ' is a '. gettype($int_as_string) . "\n";

You could also take advantage of PHP's implicit type conversion and string interpolation

$int = 5;
echo $int . ' is a '. gettype($int) . "\n"; 

$int_as_string = "$int";
echo $int_as_string . ' is a '. gettype($int_as_string) . "\n"; 

$string_int = $int.'';
echo $int_as_string . ' is a '. gettype($int_as_string) . "\n";

Finally, similar to the above, any function that accepts and returns a string could be used to convert and integer. Consider the following

$int = 5;
echo $int . ' is a '. gettype($int) . "\n"; 

$int_as_string = trim($int);
echo $int_as_string . ' is a '. gettype($int_as_string) . "\n";

I wouldn't recommend the final option, but I've seen code in the wild that relied on this behavior, so thought I'd pass it along.

Alan Storm
A: 

Thanks to all but I found 30 seconds after posting

kman99
A: 

As the answers here demonstrates nicely, yes there are several ways. However, in php you rarely actually need to do that. The "dogmatic way" to write php is to rely on the language's loose typing system, which will transparently coerce the type as needed. For integer values, this is usually without trouble. You should be very careful with floating point values though.

troelskn