Converting an integer to a string in PHP

PhpStringCastingType ConversionInteger

Php Problem Overview


Is there a way to convert an integer to a string in PHP?

Php Solutions


Solution 1 - Php

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;

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

// String concatenation 
echo "I'd like ".$var." waffles"; // I'd like 5 waffles

// The two examples above have the same end value...
// ... And so do the two below

// Explicit cast 
$items = (string)$var; // $items === "5";

// Function call
$items = strval($var); // $items === "5";

Solution 2 - Php

There's many ways to do this.

Two examples:

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

See the PHP Manual on Types Juggling for more.

Solution 3 - Php

$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"

Solution 4 - Php

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

The traditional computer science 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.

Solution 5 - Php

All these answers are great, but they all return you an empty string if the value is zero.

Try the following:

    $v = 0;

    $s = (string)$v ? (string)$v : "0";

Solution 6 - Php

Use:

$intValue = 1;
$string = sprintf('%d', $intValue);

Or it could be:

$string = (string)$intValue;

Or:

settype(&$intValue, 'string');

Solution 7 - Php

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;

Solution 8 - Php

There are many possible conversion ways:

$input => 123
sprintf('%d',$input) => 123
(string)$input => 123
strval($input) => 123
settype($input, "string") => 123

Solution 9 - Php

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.

Solution 10 - Php

My situation :

echo strval("12"); => 12
echo strval("0"); => "0"

I'm working ...

$a = "12";
$b = "0";
echo $a * 1; => 12
echo $b * 1; => 0

Solution 11 - Php

I would say it depends on the context. strval() or the casting operator (string) could be used. However, in most cases PHP will decide what's 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 :)

Solution 12 - Php

$amount = 2351.25;
$str_amount = "2351.25";
	
$strCorrectAmount = "$amount";
echo gettype($strCorrectAmount);    //string

So the echo will be return string.

Solution 13 - Php

I tried all the methods above yet I got "array to string conversion" error when I embedded the value in another string. If you have the same problem with me try the implode() function. example:

$integer = 0;    
$id = implode($integer);    
$text = "Your user ID is: ".$id ;

Solution 14 - Php

You can simply use the following:

$intVal = 5;
$strVal = trim($intVal);

Solution 15 - Php

$num = 10;
"'".$num."'"

Try this

Solution 16 - Php

$integer = 93;
$stringedInt = $integer.'';

is faster than

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkman99View Question on Stackoverflow
Solution 1 - PhpChris ThompsonView Answer on Stackoverflow
Solution 2 - PhpSanjay ShethView Answer on Stackoverflow
Solution 3 - PhpSevView Answer on Stackoverflow
Solution 4 - PhpAlan StormView Answer on Stackoverflow
Solution 5 - PhpMicroprocessor CatView Answer on Stackoverflow
Solution 6 - PhpgewelView Answer on Stackoverflow
Solution 7 - PhpAndrew DunkmanView Answer on Stackoverflow
Solution 8 - PhpUserBSS1View Answer on Stackoverflow
Solution 9 - PhptroelsknView Answer on Stackoverflow
Solution 10 - Phptemur UsmonaliyevView Answer on Stackoverflow
Solution 11 - PhpmerkuroView Answer on Stackoverflow
Solution 12 - PhpKaushik shrimaliView Answer on Stackoverflow
Solution 13 - PhpJoseph TamView Answer on Stackoverflow
Solution 14 - PhpIytiView Answer on Stackoverflow
Solution 15 - PhpparadoxView Answer on Stackoverflow
Solution 16 - PhpArthur KushmanView Answer on Stackoverflow