Is there a way to convert an integer to a string in PHP?
-
12PHP is loosely typed. What was an integer once, can be a string as well, e.g. when you `echo` it (used in so called *string context*). – hakre Jun 14 '12 at 15:01
-
3@hakre yes php is loosely typed and echoing would print the value in string context . But this does not change the datatype of a variable internally . Hence , strval($variable) is correct . – nice_dev Aug 31 '15 at 11:06
-
3@Vivek: `strval()` doesn't change the `$variable` internally neither. – hakre Aug 31 '15 at 13:00
-
4I meant $variable = strval($variable); – nice_dev Sep 01 '15 at 06:52
15 Answers
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";

- 1,667
- 9
- 27

- 16,203
- 9
- 45
- 62
-
2Good point. I threw in some mysql_escape_string functions in there to clean it up. – Chris Thompson Jun 23 '09 at 23:33
-
6edited since mysql_escape_string is deprecated since it ignores charset. – Kzqai Sep 08 '11 at 21:48
-
1Here is a live version - [link](http://init.me/186784/converting-an-integer-to-a-string-in-php) – Yash Kumar Apr 09 '12 at 04:48
-
-
@freaky: $var + $var = 10, $var . $var = "55". "." is the string concatenation operator while "+" is the addition operator. – Chris Thompson Jan 17 '13 at 18:26
-
7@Kzqai for anyone who looks at this example to try and avoid injection attacks, I'd say abandon your attempts at escaping strings and use prepared statements – chacham15 Jan 11 '15 at 23:46
-
26After 5 years I finally removed the SQL example as it was unnecessary to answer the question and introduced confusion. – Chris Thompson Jan 13 '15 at 07:18
-
1Here is a benchmark, for the examples in the answer: http://leifw.wickland.net/2009/08/performance-of-converting-integer-to.html – Kim Hogeling Feb 05 '15 at 12:24
-
-
8Warning!! If you pass in certain numbers php will round them when converting to string: `strval(0.999999997)` returns `1`. – danielson317 Oct 26 '18 at 18:48
-
1Can anyone provide a reason to choose a cast over a function call or vice versa? – ADJenks Feb 05 '22 at 00:13
-
@ADJenks cast is theoretically faster because of the overhead of making a function call. – Buttle Butkus Jan 06 '23 at 09:39
There's many ways to do this.
Two examples:
$str = (string) $int;
$str = "$int";
See the PHP Manual on Types Juggling for more.

- 1,539
- 1
- 9
- 9
$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"

- 30,738
- 21
- 105
- 131

- 15,401
- 9
- 56
- 75
-
-
I think you're mixing up your concatenation syntax between languages there. – jason Jun 23 '09 at 22:40
-
8I'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 Jun 23 '09 at 22:48
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.

- 30,738
- 21
- 105
- 131

- 164,128
- 91
- 395
- 599
Use:
$intValue = 1;
$string = sprintf('%d', $intValue);
Or it could be:
$string = (string)$intValue;
Or:
settype($intValue, 'string');

- 156,878
- 40
- 214
- 345

- 411
- 7
- 7
Warning: the below answer is based on the wrong premise. Casting 0 number to string always returns string "0", making the code provided redundant.
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";

- 156,878
- 40
- 214
- 345

- 117
- 1
- 2
-
2Note that the question specifically says 'converting an integer', not a float. :) – Microprocessor Cat Apr 10 '15 at 16:37
-
1Correct the mistake, please. The correct form should be: $s = (string)$v ? (string)$v : "0"; – nintyfan Dec 26 '18 at 10:07
-
1`var_dump((string)0);` prints `string(1) "0"` on PHP 5.5.9 (Released: 6 Feb 2014) for me. Which version of PHP did you get "an empty string if the value is zero"? – Lacek Jun 15 '21 at 03:06
There are many possible conversion ways:
$input => 123
sprintf('%d',$input) => 123
(string)$input => 123
strval($input) => 123
settype($input, "string") => 123

- 2,091
- 1
- 28
- 31
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;

- 30,738
- 21
- 105
- 131

- 1,131
- 2
- 8
- 18
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.

- 30,738
- 21
- 105
- 131

- 115,121
- 27
- 131
- 155
-
1I don't think that PHP will coerce an integer to string all the time. It will do it for `strlen(12345);` but not for `$x = 12345; echo $x[2];` All these casting functions are quite useful and lots of programmers are checking their types more and more. – Elzo Valugi Aug 18 '16 at 22:01
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 :)

- 30,738
- 21
- 105
- 131

- 6,161
- 2
- 27
- 29
$amount = 2351.25;
$str_amount = "2351.25";
$strCorrectAmount = "$amount";
echo gettype($strCorrectAmount); //string
So the echo will be return string.

- 1,178
- 8
- 15
-
Re "**string**": literally or the type? Or something else? Can you make it more clear (by [editing your answer](https://stackoverflow.com/posts/54325809/edit))? – Peter Mortensen Sep 12 '19 at 22:25
-
Thanks for the comment@ Peter Mortensen That is the return type "STRING" means your value should be converted as a string in a new variable. so that's why I edited string – Kaushik shrimali Sep 13 '19 at 05:28
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

- 156,878
- 40
- 214
- 345
-
1As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 07 '22 at 11:25
-
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 ;

- 3,073
- 7
- 20
- 33

- 1
- 1
- 2
-
1This code makes no sense and returns an error, `Argument #1 ($pieces) must be of type array, string given` – Your Common Sense Jun 03 '22 at 18:32
You can simply use the following:
$intVal = 5;
$strVal = trim($intVal);

- 27,240
- 15
- 95
- 114

- 7
- 5
-
1The `trim()` function removes whitespace and other predefined characters from both sides of a string Although it returns string it's a **bad practice**. It's better to use `strval()` or `number_format()` perhaps. – Marwan Salim Oct 02 '20 at 06:57
$integer = 93;
$stringedInt = $integer.'';
is faster than
$integer = 93;
$stringedInt = $integer."";

- 30,738
- 21
- 105
- 131

- 84
- 1
- 2
-
2$foo = 5; $foo = "$foo" extremely waistfull for memory in PHP use ''. – Arthur Kushman Jan 21 '11 at 13:38
-
What do you mean by wasteful? ``` $foo = 5; $foo = "$foo"; ``` This code is just overwriting int value with string, of course, it's not the answer but what's wasteful? – JEX Oct 24 '21 at 01:15